Question:
Java - cant get file to copy contents to another file?
2013-03-18 06:10:21 UTC
I'm creating an address book holding contacts information. I'm reading this data from a csv file. I've read the file fine, but I want to be able to add contacts, search contacts, delete contacts, and edit contacts. Then copy any changes to another file. This is where I'm having the issues. Both files are in the right directory, and the addresses.csv file (the one I'm reading from) is read fine and prints contacts to screen. However, when I'm trying to get the data to another file, the file remains blank, what am I doing wrong.
Still new to this whole programming thing, it's only been about 2 months, so I know the way I'm doing this program probably isn't't the best way, but I dont have enough time to change everything now.

Please any help would be greatly appreciated...



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Main
{
public static void main (String [] args) throws FileNotFoundException
{
String [] lName, fName, street, city, state, zip, phone;

lName = new String[20];
fName = new String[20];
street = new String[50];
city = new String[20];
state = new String[20];
zip = new String[12];

System.out.println("ADDRESS BOOK CONTENTS: ");

Scanner inFile;

try
{
inFile = new Scanner (new File("src/addresses.csv"));
String temp;

while (inFile.hasNextLine())
{
temp = inFile.nextLine();
System.out.println("Contact: " + temp);
}

System.out.println(inFile);


}

catch (FileNotFoundException e)
{
//catch block
e.printStackTrace();
}
try
{
FileInputStream fileIn = new FileInputStream("src/addresses.csv");
FileOutputStream fileOut = new FileOutputStream("src/newfile.csv");

int c;
while ((c = fileIn.read()) != -1)
{
fileOut.write(c);
}

fileIn.close();
fileOut.close();
}

catch (FileNotFoundException exception1)
{
System.err.println("FileCopy: " + exception1);
}
catch (IOException exception1)
{
System.err.println("FileCopy: " + exception1);
}

}
}



Three answers:
Ansque
2013-03-18 06:39:51 UTC
I tried to run your code. It worked just fine. The file perfectly copied all the content to the other file.



Only the line,

System.out.println(inFile);



Printed something BIGGG :) Though it was an object, but its on the console.



The newfile.csv, was exactly same as addresses.csv after the execution.



Can you explore more, what you see on the console or something which you see fishy?



Update:



In my case the the newfile.csv got created in the same src folder where i placed the addressed.csv



Can you try it out side without using your IDE, just the java file and the src folder with its contents.
husoski
2013-03-18 13:56:18 UTC
It works just fine here. I pasted the code into a NetBeans project so I could re-indent the source. That, and renaming the class from Main to JavaTest (to match the source file in a project I keep around for things like this) were the only changes I made. Then I added a src\addresses.csv file (just two lines) to the project and ran the code. It successfully created an exact copy of the input file.



It's a byte-by-byte copy and (as expected) it did not add a trailing '\n' when I left it off the last input line on the second run. So, that test works too.



This is on Win7 32-bit, JDK 1.7.0_11, NetBeans 7.2.1, if that matters.



A couple of thoughts: If you have the output file opened in an editor in another window, some editors will keep the file opened for output during the edit session. That will prevent any access by another process. Also, some editors that don't have that problem may need you to close the file (without saving!) and reopen the file in order to see changed contents. This happens if the editor doesn't monitor the file system to detect changes to the file.



---- Edit, regarding details:



The output "newfile.csv" file was created in the same "src\" directory as the input.



As for the long output line, that's caused by System.out.println(inFile). I'm not sure what you intended, but since inFile is an object, println() prints out whatever is returned by that object's toString() method. For objects that don't have a natural string representation, the usual convention is to return a string with formatted debugging information. That's what FileInputStream seems to do.
Raj C
2013-11-30 12:59:06 UTC
Copy Content of One file to another file using Java



To Get sample Code click here
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...