Link to home
Start Free TrialLog in
Avatar of ukapu2005
ukapu2005

asked on

please provide me the java code to write into a file and then I should able to read that info from file

I want to write the following info onto a file that is located in my d drive. Can any body provide me the java code.

CUSTORDR - "13007","","","0.0","C"
ORDERDET - "12002","item1d","1.0","100.0","100.0"
ORDERDET - "11503","item1b","1.0","100.0","100.0"
ORDERDET - "11503","item1b","4.0","100.0","400.0"
SHIPCHRG - "0.0","0"
SALESTAX - "0.0","0.0"

Avatar of zzynx
zzynx
Flag of Belgium image

So that's:

try {
        BufferedWriter out = new BufferedWriter(new FileWriter("d:/dir1/MyFile.txt"));
        out.write("theLineyouWant1");
        out.write("theLineyouWant2");
        out.write("theLineyouWant3");
        out.write("theLineyouWant4");
        out.write("theLineyouWant5");
        out.close();
    } catch (IOException e) {
    }
String s = ...(whatever You need)

//writing to file
FileOutputStream fout = new FileOutputStream("D:\filename");
fout.write(s.getBytes());
fout.close();

//reading
FileInputStream fin = new FileInputStream("D:\filename");
byte[] contents = new byte(fin.available());
fin.read(contents);
String s = new String(contents);
If you want your file exactly to contain the line

  CUSTORDR - "13007","","","0.0","C"

then use

  out.write("CUSTORDR - \"13007\",\"\",\"\",\"0.0\",\"C\"");
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops, zzynx You're right :)
Avatar of sciuriware
sciuriware

This question seems to be popular because it's asked more that once.
From which book?

;JOOP!
Thanks for accepting.

(For the future: If you're not completely satisfied with a comment - that's how I interprete a B-grade - you can always ask for further explanation)