[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

How to write lline by line using fileoutput Stream

How to write lline by line using fileoutput Stream

give me  a code for that
0
Manikandan Thiagarajan
Asked:
Manikandan Thiagarajan
  • 6
  • 3
  • 2
1 Solution
 
objectsCommented:
PrintWriter printer = new PrintWriter(new OutputStreamWriter(out));
printer.println("line1");
printer.println("line2");
0
 
objectsCommented:
where 'out' is your FileOutputStream
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
i want to write into the file line by line using fileoutputstream
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
thats what I posted above:

FileOutputStream out = new FileOutputStream("myfile.txt");
PrintWriter printer = new PrintWriter(new OutputStreamWriter(out));
printer.println("line1");
printer.println("line2");
out.close();
0
 
objectsCommented:
you wrap the fileoutputstream in a PrintWriter which provides the ability to write line by line
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
this code is not able to write the file using fos
clobReader =psmt.getClobQueryData();
              
             // 
              FileOutputStream fos=new FileOutputStream(file);
              PrintWriter printer = new PrintWriter(new OutputStreamWriter(fos));
              if(!file.exists()){
                       file.createNewFile();
              System.out.println("Query is working fine");
                 }
              String temp=clobReader.readLine();
              
              BufferedWriter bw = new BufferedWriter(fw);
              //while(i=0)
              while(temp!=null)
              {
                  
              System.out.println("the String is..."+temp);
              
              //System.out.println("ByteSize"+b.length);
          //   fos.write(temp.getBytes());
             printer.println(temp);
            // printer.write(temp);
             // printer.write("\r\n");
            //  fos.flush(); 
              temp=clobReader.readLine();

Open in new window

0
 
objectsCommented:
             if(!file.exists()){
                       file.createNewFile();


you don't need thoses lines

and you don't close the stream
0
 
CEHJCommented:
If the source of 'clobReader' already contains lines, there's no point in reading a copying lines separately - this is pointless and makesthe copy inefficient. Just copy the Reader directly. See http://technojeeves.com/joomla/index.php/free/51-copying-streams


IOUtils.copyReader(clobReader, new FileWriter(file));

Open in new window

0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
please give me the example with file output stream

in my program only fileoutputstream is working
0
 
CEHJCommented:
Did you try the code i posted?
0
 
objectsCommented:
> please give me the example with file output stream

I already did. Please explain exactly what problem you are having with the code I posted
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now