Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Tolls

Thread: Problem with java.io.FiileWriter

  1. #1
    Filoflow is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Problem with java.io.FiileWriter

    I'm having a problem when writing the output.txt
    The code is suposed to create the file output.txt (which does fine) and then write all that is read from input.txt (not so fine).
    There are no runtime errors.


    Java Code:
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Coder {
    	
    	static Scanner fileScanner;
    	static String PATH="C:/";
    	static String filePath;
    	static File fileRead;
    	static FileWriter fileWriten;
    	
    	
    	public static void main(String[] args){
    		fileRead = new File("C:/input.txt");
    		try {
    			fileScanner = new Scanner(fileRead);
    			fileWriten = new FileWriter("C:/output.txt");
    			BufferedWriter bw = new BufferedWriter(fileWriten);
    			while(fileScanner.hasNext()){
    				bw.write(fileScanner.next());
    			}
    			
    		} catch (IOException e) {
    
    			e.printStackTrace();
    		} 
    	}
    	
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem with java.io.FiileWriter

    Since you are doing no work on the data at all then just use FileInputStream and FileOutputStream and read a byte[] buffer and write that buffer.

    Your Scanner will be removing spaces and new lines, so is no use to you at all, frankly.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Filoflow is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: Problem with java.io.FiileWriter

    Quote Originally Posted by Tolls View Post
    Since you are doing no work on the data at all then just use FileInputStream and FileOutputStream and read a byte[] buffer and write that buffer.

    Your Scanner will be removing spaces and new lines, so is no use to you at all, frankly.
    Actually this is the part of the code that is giving me troubles, but i do need to be capable to modify the data. The program is suposed to "codify" or "translate" the text, so that's why I don't think that bytes will do :S

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Problem with java.io.FiileWriter

    Hi filoflow, welcome to the forums.

    The BufferedWriter stores the input in a buffer before writing it to the underlying stream (the output file). To cause it to write to the stream you need to call either the flush() or close() methods. close() is probably more applicable.

    Regards.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem with java.io.FiileWriter

    Still ditch the Scanner as you are losing spaces.
    Use a BufferedReader and readline, and use a PrintWriter so you can use the println() method when writing each line.
    Also implement the close() that Ronin mentions, otherwise it won't write to the file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Problem with java.io.FiileWriter

    Following on from Toll's suggestion, an example of PrintWriter with BufferedReader can be seen in the BufferedWriter API.
    BufferedWriter (Java 2 Platform SE 5.0)

    Regards.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem with java.io.FiileWriter

    Or even:
    Java 7 version.

    (Part of my ongoing battle to get Java 7 docs higher in the Google rankings).
    Ronin likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Filoflow is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: Problem with java.io.FiileWriter

    Thank you very much guys, the bw.close() made it work.
    Anyway I will start using the PrintWriter and BufferedReader rather than the Scanner class because of the spaces.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Problem with java.io.FiileWriter

    Actually, you could use the Scanner class if you use nextLine() and not next(). But you would lose the line terminator and would have to tack it back on. PrintWriter and BufferedReader are the way to go. Scanner is more often used for interactive input.

    Regards,
    Jim
    Last edited by jim829; 03-12-2013 at 04:43 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem with java.io.FiileWriter

    You lose the line terminator with the BufferedReader as well, on readLine().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Problem with java.io.FiileWriter

    Quote Originally Posted by jim829 View Post
    Scanner is more often used for interactive input.
    I'd say it's best used for mixed input. When reading from a flat file used as a poor man's database, for example, next() / nextInt() / nextDouble() make it so much easier to get the data fields and construct an instance of a class that represents the data in one line of the file, or otherwise make use of that data.

    This is actual code from a Houise (aka Tombola) game I wrote:
    Java Code:
        Scanner scanner = new Scanner(GameModel.class.getResourceAsStream("/res/FancyCalls.txt"));
        while (scanner.hasNextLine()) {
          if (scanner.hasNextInt()) {
            int number = scanner.nextInt();
            standardCalls.get(number).add(scanner.nextLine().trim());
          } else {
            // allows intermediate blank or comment lines
            scanner.nextLine();
          }
        }
        scanner.close();
    Typical line in the text file:
    Java Code:
    50 Half a century
    db

    edit standardCalls is a Map<Integer, List<String>>
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Problem with java.io.FiileWriter

    Of course, which is why you said use PrintWriter (and not BufferedWriter). Duh..

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 3
    Last Post: 09-01-2012, 08:58 PM
  2. Replies: 4
    Last Post: 03-16-2012, 11:29 AM
  3. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  4. Need Help!:( Java problem
    By ra21 in forum New To Java
    Replies: 2
    Last Post: 02-01-2011, 10:41 PM
  5. Problem Display Jmenubar Java Se6 u23 versus Java SE6 u22
    By Ravanelly in forum Advanced Java
    Replies: 0
    Last Post: 01-07-2011, 09:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •