• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need A hand With my home work (write to a file)

 
JAY MaCaNaWallChucK
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all can some one plz help me with this for some reason this compiles fine but it wont write to the file I tell it to im stumped....this files are in the same directory as the .java file..... if you have the awnser plz post some code to help me get my mind wraped around this concept...thkz
import java.io.*;
import java.util.*;
public class FileCopier {

public static void main(String[] args) {
/*if(args.length != 2) {
System.err.println("Incorrect number of parameters");
return;
)*/

try{

File inputFile = new File("employees.txt");
File outputFile = new File("empout.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter("empout.txt");

BufferedReader inputStream = new BufferedReader(in);
BufferedWriter outputStream = new BufferedWriter(out);

String line = "";



int count = 0 ;

while (true) {
count++;
//System.out.println(count);

line = inputStream.readLine();

//breaks out of the loop if the file is finished
if (line == null) {
break;
}
String empNum = line.substring(1,6);
String fstName = line.substring(6,21);
String lstName = line.substring(21,46);
String depNum = line.substring(46,49);
String hiDate = line.substring(49,57);
String salary = line.substring(57,63);

outputStream.write(empNum);


System.out.println (empNum);
System.out.println (fstName);
System.out.println (lstName);
System.out.println (depNum);
System.out.println (hiDate);
System.out.println (salary);



}

} catch (FileNotFoundException fnfe) {
System.out.println("The File Was Not Found");
return;

} catch (IOException ioe) {
}
}


}
N e help on how i can get this to write to file would be great
 
Mahesh Mamani
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You haven't closed the BufferedWriter object.
Do this n Ur prob is solved.
System.out.println (hiDate);
System.out.println (salary);
outputStream.close();
Bye

Mahesh
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay-
Welcome to the JavaRanch! Please adjust your displayed name to meet the JavaRanch Naming Policy. You can do so here. Primarily, obviously fictitious names are not allowed. Don't have to use your real last name, but something other than what you have right now
Thanks! and again, Welcome to the Ranch!
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Mahesh said, the number one problem I run into when for some reason my program is not writing is I forget to flush! When you close the writer, it automatically flushes for you. You can also call the flush() method, which I religiously do now. Gives me a better feeling than letting close() do it.
 
JAY MaCaNaWallChucK
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thkz for the help the flush and the close worked and it writes tio the file now.....you guys are the best thkz again..................
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!