4 Replies - 1272 Views - Last Post: 03 April 2011 - 02:08 PM Rate Topic: -----

#1 VonNemo19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-May 10

I'm trying to write to a file using the printwriter class.

Posted 03 April 2011 - 06:22 AM

Hello, I'm having trouble getting the desired output here. Everything that this program is supposed to do works except that I'm not getting any output to the desired file. I will post the instructions to the problem and then my code. If you need me to give more nfo, just ask. Thanks to anyone for helping me. :)

Instructions:

Write a program that asks the user for the names of 2 files. The first file should be opened for reading and the second file should be opened for writing. The program should read the contents of the first file, change all characters to uppercase, and store the results in the second file.The second file will be a copy of the first, except that all of the characters will be uppercase. Use notepad or another text editor to create a simple file that can be used to test the program.

my attempt:

import java.io.*;
import javax.swing.*;
import java.util.*;

public class UppercaseFileConverter
{
  public static void main(String[] args) throws IOException
  {
    
    String fileName = "";
    String uppercaseLines = "";
    
    //Get the name of the first file from the user
    fileName = JOptionPane.showInputDialog("Please specify the name of the file"
                                         + " you wish to read from:");
    
    //Create and open the file for reading
    File file = new File(fileName);
    Scanner inputFile = new Scanner(file);
    
    //While there is another line to read, the program will concatenate
    //the line under the uppercaseLine variable in all uppercase 
    while (inputFile.hasNext())
    {
      String line = inputFile.nextLine();
      uppercaseLines += "\n"+line.toUpperCase();
    }

    //Get the name of the output file from the user
    fileName = JOptionPane.showInputDialog("Please specify which file you would like to write to: ");
    
    //Create and opent the file for writing
    FileWriter fWrite = new FileWriter(fileName,true);
    PrintWriter outputFile = new PrintWriter(fWrite);
    

    //This method should write the changed text to the file but it doesn't seem to be doing this...
    outputFile.println(uppercaseLines);
    

    //Close the files
    inputFile.close();
    outputFile.close();
  }
}


Like I said, the problem is that there is nothing written to the output file. The file is created, but there is no text written to it. What's the problem?

This post has been edited by VonNemo19: 03 April 2011 - 06:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: I'm trying to write to a file using the printwriter class.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: I'm trying to write to a file using the printwriter class.

Posted 03 April 2011 - 06:44 AM

Worked fine for me, except you appear to be missing the comment //s on line 17
Was This Post Helpful? 1
  • +
  • -

#3 VonNemo19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-May 10

Re: I'm trying to write to a file using the printwriter class.

Posted 03 April 2011 - 06:53 AM

Thanks. I think the problem was that I didn't close the files that were created within the program and right before I uploaded this post, I put the code in to close the files and I didn't think that would have anything to do with the output.

THanks anyway.
Was This Post Helpful? 0
  • +
  • -

#4 VonNemo19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-May 10

Re: I'm trying to write to a file using the printwriter class.

Posted 03 April 2011 - 06:58 AM

View PostJackOfAllTrades, on 03 April 2011 - 06:44 AM, said:

Worked fine for me, except you appear to be missing the comment //s on line 17

Hey, why does all the output go to one line in the output file? I used the escape \n in the loop. What's the problem here?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: I'm trying to write to a file using the printwriter class.

Posted 03 April 2011 - 02:08 PM

This

FileWriter fWrite = new FileWriter(fileName,true);

won't create a new file as your comments says
It will append to an existing file, remove ",true"

Once that done it works for me :^:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1