FILE I/O TXT file

Hi.. i have a small problem... i have this file marks with the following contents:

1234567//Student ID

ENG3U//Course

86//Marks

2345671

MAT3C

45

1122334

ICS3M

78

5656453

ICE3M

67

Total of 3 students. What my program is supposed to do is that check the mark of the student for pass/fail. If he/she passes, only output the course and the mark to a pass.txt file and vice versa with fail.txt.

Right now, my program is not running. I am able to load the marks.txt file but when i try to write contents into another file for beginners, it doesnt output anything but makes a file... here's the code:

import java.io.*;

publicclass program6{

publicstaticvoid main (String args[])throws IOException{

System.out.println ("MARKING");

FileReader fr =new FileReader ("marks.txt");

BufferedReader br =new BufferedReader (fr);

//reading the contents of the file.

String a = br.readLine();

while (a !=null){

//call the pass fail method with contents.

passFail(a);

a = br.readLine();

}

a = br.readLine();

}

publicstaticvoid passFail(String line)throws IOException{

//PROBLEM HERE. IT IS NOT WRITING THE CONTENTS OF marks.txt to the testing.txt file.

File f =new File ("testing.txt");

BufferedWriter wr =new BufferedWriter (new FileWriter(f));

wr.write(line);

wr.close();

}

}

Can anyone help me

[2595 byte] By [MichaelPanksa] at [2007-11-27 11:44:06]
# 1

every time you write a new line you are overwriting the file

over and over.

put this line:

System.out.println("Line: " + line)

into the passFail method

and you will see that you are writing every line.

TuringPesta at 2007-7-29 17:53:43 > top of Java-index,Java Essentials,New To Java...
# 2

Every time you call passFail, you overwrite testing.txt. Could it be that the last time you call this method, you pass it the empty string?

BigDaddyLoveHandlesa at 2007-7-29 17:53:43 > top of Java-index,Java Essentials,New To Java...
# 3

> put this line:

> System.out.println("Line: " + line)

> into the passFail method

> and you will see that you are writing every line.

I tried that, it prints out every line.. but when i make a filewriter global, i had to make it static. Even then, it didn't copy a single line of marks.txt into the new file..

import java.io.*;

public class program6 {

static File f = new File ("testing.txt");

static BufferedWriter wr = new BufferedWriter (new FileWriter(f));

public static void main (String args[]) throws IOException {

and in the method:

public static void passFail(String line)throws IOException {

wr.write(line);

wr.close();

}

MichaelPanksa at 2007-7-29 17:53:43 > top of Java-index,Java Essentials,New To Java...
# 4

import java.io.*;

public class Program6{

public static void main(String args[]){

new Program6();

}

public Program6(){

try{

File file = new File ("testing.txt");

BufferedWriter wr = new BufferedWriter(new FileWriter(file));

FileReader fr = new FileReader ("marks.txt");

BufferedReader br = new BufferedReader (fr);

String line;

while((line = br.readLine()) != null){

passFail(wr, line);

}

br.close();

fr.close();

wr.close();

} catch(Exception e){

e.printStackTrace();

}

}

public void passFail(BufferedWriter wr, String line) throws IOException{

System.out.println("Line: " + line);

wr.write(line);

}

}

TuringPesta at 2007-7-29 17:53:43 > top of Java-index,Java Essentials,New To Java...
# 5

thanks.. it worked.. eventhough it printed everything on a single line....

MichaelPanksa at 2007-7-29 17:53:43 > top of Java-index,Java Essentials,New To Java...
# 6

> eventhough it printed everything on a single line....

left as an exercise for the interested student

BigDaddyLoveHandlesa at 2007-7-29 17:53:43 > top of Java-index,Java Essentials,New To Java...
# 7

Read

http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedWriter.html

TuringPesta at 2007-7-29 17:53:43 > top of Java-index,Java Essentials,New To Java...
# 8

that was a joke... its working fine.. thanks man...

MichaelPanksa at 2007-7-29 17:53:43 > top of Java-index,Java Essentials,New To Java...
# 9

haha dont tell me you're stuck on this too?

stupid online course and absentee teacher

i made another similar thread...

irutaviasa at 2007-7-29 17:53:43 > top of Java-index,Java Essentials,New To Java...