Results 1 to 8 of 8
Thread: Reading files
- 08-15-2011, 10:36 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 11
- Rep Power
- 0
Reading files
So here we go, hope this is an alright place to post this.
It's very much a personal project, just something I've set myself to try and further my knowledge of java. I only started studying it this year so I'm pretty useless so far.
Basically, the end aim is to develop a piece of code that will go through a file, copy all the lines that begin with a number and then insert them into a new file.
I've started off with a piece of code that will read through a file and display the letter "Y" when the line begins with an 'A' and display the letter "N" when it begins with anything else. The only way I know how to go through an entire file is to use:
while(MyFile.readLine()!=null)
However, I want it to be able to process lines that have nothing on them.
The code I've got so far is:
body.txt is simplyimport java.io.*;
class test1
{
public static void main(String[] args) throws IOException
{
FileReader file = new FileReader("body.txt");
BufferedReader MyFile = new BufferedReader(file);
String text = MyFile.readLine();
while(text!=null)
{
if(text.charAt(0)=='A')
{
System.out.println("Y");
}
else
{
System.out.println("N");
}
text = MyFile.readLine();
}
MyFile.close();
}
}
However, if there is an empty line in the middle of body.txt, it obviously won't work. Any help would be greatly appreciated!A
B
B
A
B
B
A
A
A
Many thanks.
- 08-15-2011, 11:12 PM #2
I actually think what you have should work. No reason why a blank line should stop the loop. It will read the entire file. So for instance, if you have a file with 7 lines:
A
B
C
D
E
F
It will evaluate all the way to the F. Heres a sample of the File reader, but it's very similar to what you have. What output are you seeing?
That should produce the following output:Java Code:try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("testing.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { System.out.println(strLine); } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); }
Java Code:A B C D E F
- 08-16-2011, 12:15 AM #3
Please explain the reason it won't work.it obviously won't work
You can test the contents of the String that you read into to see if it is empty. See the String API for methods to use.I want it to be able to process lines that have nothing on them.
- 08-16-2011, 12:23 AM #4
It won't work because a blank line does not have a char at index 0. Op will need to add a separate test for a blank line, such as testing the length of the String read in.
- 08-16-2011, 12:26 AM #5
Yes, I saw that. I was wondering if that was the reason for the message the OP posted. You never know what a student thinks his code does or does not do. So I ask to be sure we are on the same page.
You'll note I recommended that the String be tested to see if it is empty which I thought would be a solution for the OPs problem.
- 08-16-2011, 12:31 AM #6
Oops. I really have to read entire posts. ;)
- 08-16-2011, 02:09 AM #7
The blank line will result in a zero length String so charAt( 0 ) will bomb.
I would test to see if the length of the text string is greater than 0 before seeing what the first character is.
Refer to the String class.If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-16-2011, 02:18 AM #8
Similar Threads
-
Reading from .txt files
By rileannas in forum Java AppletsReplies: 4Last Post: 05-10-2011, 05:16 PM -
Bug Reading txt files
By MHardeman25 in forum New To JavaReplies: 4Last Post: 08-13-2010, 10:03 PM -
Reading .bin files
By spatel14 in forum New To JavaReplies: 3Last Post: 06-22-2010, 04:39 PM -
Reading .txt files
By cvcs1 in forum New To JavaReplies: 3Last Post: 01-20-2010, 09:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks