Results 21 to 33 of 33
Thread: Please Help
- 01-02-2009, 07:12 AM #21
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-04-2009, 06:13 AM #22
Member
- Join Date
- Jan 2009
- Posts
- 26
- Rep Power
- 0
- 01-04-2009, 04:58 PM #23
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you try it what happen? I think you only get the last line of the original file to the new one, is it?
You cannot identify why it's happen?
Look at the following two lines of code of the writeToFile() method.
Once read a line from the original file you call that writeToFile() method with it. Each time you initiate a new PrintWriter object, and what happen is new file is created and replace the original.Java Code:PrintWriter output; output = new PrintWriter (new FileWriter ("C:/Poem2.txt"));
- 01-04-2009, 08:30 PM #24
Member
- Join Date
- Jan 2009
- Posts
- 26
- Rep Power
- 0
- 01-05-2009, 04:19 AM #25
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Initiate the PrintWriter object only once. You can do it within the constructor. Define it in class level.
- 01-05-2009, 04:50 AM #26
Member
- Join Date
- Jan 2009
- Posts
- 26
- Rep Power
- 0
- 01-05-2009, 05:10 AM #27
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Better to read more about those things before work out.
Java Code:import java.io.*; import java.util.Scanner; /** * * @author Eranga Tennakoon */ public class NumberedPoem { static Scanner input; static PrintWriter output; public NumberedPoem() throws IOException { input = new Scanner( System.in); output = new PrintWriter (new FileWriter ("C:/Poem2.txt")); } public static void main (String[] args) throws IOException { // Declare variables String fileName, line; int number; number = 0; System.out.print("What is the name of the data file? "); // Open up Poem fileName = input.nextLine(); BufferedReader bufReader; bufReader = new BufferedReader (new FileReader (fileName)); while ((line = bufReader.readLine ()) != null) { //Loop terminates at end of file number++; System.out.println(number + " " + line); writeToFile(line); } } public static void writeToFile(String textLine) throws IOException { output.write(textLine); output.write(System.getProperty("line.separator")); // New line output.flush(); } }
- 01-05-2009, 06:10 AM #28
Member
- Join Date
- Jan 2009
- Posts
- 26
- Rep Power
- 0
Thanks for your help...only problem is Ready to Program Java IDE does not support the import java.util.Scanner;
- 01-05-2009, 06:19 AM #29
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I don't know why your application doesn't support default JDK packages. Did you get any error messages?
- 01-05-2009, 06:28 AM #30
Member
- Join Date
- Jan 2009
- Posts
- 26
- Rep Power
- 0
- 01-05-2009, 08:04 AM #31
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
A Classpath problem?
It simply can't be :DReady to Program Java IDE does not support the import java.util.Scanner
Maybe you're IDE didn't recognize your JDK installation. Check your CLASSPATH. Do you have any problem with the java.io classes?
- 01-05-2009, 08:09 AM #32
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Or may be you are using the older JDK version.
- 01-05-2009, 09:38 AM #33
Member
- Join Date
- Jan 2009
- Posts
- 26
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks