DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Thread: Writing to file

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Posts
    34

    Question Writing to file

    This module takes an int from a file, adds 10 to the amount in the txt file, then puts the results into another file.
    I can get the results to the console but not to another file?
    >>>bw.write(outLine);<<<
    How can I get this to another file. Not sure why it is not processing?

    farrago.txt == (1 3 4 5 7)

    import java.io.*;
    public class Copy {
    public static void main(String[] args) throws IOException {

    BufferedReader br=new BufferedReader(new FileReader("farrago.txt"));
    BufferedWriter bw=new BufferedWriter(new FileWriter("outagain.txt"));
    String line=null;
    int n=0;
    while ( (line = br.readLine()) != null) {
    String[] intStr = line.split(" "); // if ints are separated by space
    String outLine = "";

    for (int i = 0; i < intStr.length; i++) {
    intStr[i] = intStr[i].trim();
    String s=intStr[i];
    try {
    n = Integer.parseInt(s);
    } catch (NumberFormatException nfe) {
    System.out.println("the line: ["+line+"] failed integer conversion");
    System.out.println("Failure for :["+intStr[i]+"]");
    System.out.println(nfe.getMessage());
    System.out.println("Element was skipped");
    continue;
    }
    n=n+10;
    outLine += (n) + " ";
    }
    System.out.println(outLine);
    bw.write(outLine);
    bw.newLine();
    }
    }
    }

    Thanks....

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I can only see one thing missing....

    I think you should close the files before the program terminates. So when
    the read loop has finished you do:

    bw.close();
    br.close();

    You should always do so when you are processing files, or you can (if you are
    really in bad luck) get them pretty messed up.

    PS:
    This looks like a piece of code that I have been involved in, so if I forgot to
    include the close statements I apologise (blush).
    Last edited by sjalle; 02-27-2005 at 12:02 PM.
    eschew obfuscation

  3. #3
    Join Date
    Oct 2004
    Posts
    34

    Thumbs up Writing to file

    sjalle, no need to apologize, I should have detected it as well, thanks for the help and advice. The close methods is what it needed.
    bw.close();
    br.close();

    Thanks....!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links