4 Replies - 968 Views - Last Post: 20 March 2012 - 06:33 PM Rate Topic: -----

#1 jruss1212   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Writing to a File

Posted 20 March 2012 - 01:16 PM

Hey I'm working on a part of my program where it needs to write to a file. I need to use a file object to test that the file doesm't already exist and needs to reprompt for overwriting if it does exist. I was wondering if anyone knew of any tutorials that could help based on these instruction from my project:

Prompt for the file name of the output file to write the formatted words to. Use a File object to test that the file doesn't already exist, and if it does, prompt to check for overwriting the file. Open the file for writing using a PrintWriter. Use a do-while loop to repeat these steps until a valid output file is entered.

Loop, reading words (text between whitespace) from the input file using Scanner.next(), appending them to a line of text being formatted, with one space character between words. Keep appending words until the current word would cause the line to become longer than the output column with. If so, write the line to the output file, reset it to contain just the new word, and continue reading. Continue reading until the end-of-file is encountered, using Scanner.hasNext().


I was trying to code it based on another tutorial but that one writes a line to a file hardcoded in the program. The biggest issue I'm having is the second instruction. Here is what I put together so far:


System.out.println("Enter output file name or 'console'.");
		String outName = sc.nextLine();
		
		PrintWriter outWriter;
		try{
			outWriter = new PrintWriter(new FileWriter(outName));
			outWriter.write("");
			outWriter.close();
		}catch (IOException e){
			System.out.println(e.toString());
		}


Is This A Good Question/Topic? 0
  • +

Replies To: Writing to a File

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Writing to a File

Posted 20 March 2012 - 01:20 PM

You have two involved threads on reading from a File. Start there. How do you read in a word from a File? How do you write a single String to a File? Break down the problem into more manageable parts.
Was This Post Helpful? 0
  • +
  • -

#3 jruss1212   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Writing to a File

Posted 20 March 2012 - 02:04 PM

Well I thought I should create a new topic since that was all about reading from a file. This seems a lot harder than having the program read from a file. Is "Scanner.next()" used to read text and ignore spaces or do you have instruct it to do that? Am I not using these methods correctly? Here's new code:

System.out.println("Enter output file name or 'console'.");
		String outName = sc.nextLine();
		
		PrintWriter outWriter;
		String outWriter1 = "";
		try{
			while(outWriter.Scanner.hasNext()){
			outWriter = new PrintWriter(new FileWriter(outName));
			outWriter1 += inName.Scanner.next() + " ";
			
			}
			outWriter.close();
		}catch (IOException e){
			System.out.println(e.toString());
		}

Was This Post Helpful? 0
  • +
  • -

#4 jruss1212   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Writing to a File

Posted 20 March 2012 - 03:43 PM

Anyone available to take a look at this?
Was This Post Helpful? 0
  • +
  • -

#5 ILoveJava   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Re: Writing to a File

Posted 20 March 2012 - 06:33 PM

There's no need to bump a thread after an hour. Also please post your full program.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1