Hello, so I have this program which should be creating a file called output and appending some content from another file,data.txt, accordingly. Creating file called output works but then the output file is empty. what am i doing wrong?
Oh and Im suppose to use method print() but that gives me a bunch of errors that says its not applicable.
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileWriter;
class TestFile2{
public static void main(String[] args) {
try {
PrintWriter out = new PrintWriter(new FileWriter("output", true));
File f1 = new File("c://Java/Lab 1/MyDocument/data.txt");
File f2 = new File("MyDocument", "data.txt");
File d1 = new File("MyDocument");
if(f1.isDirectory()){
out.print(f1.getName() + " is a directory");
}else if(f1.isFile()){
out.print(f1.getName() + " is a file");
}
if(d1.isDirectory())
out.print(d1.getName() + " a directory");
if (f1.exists()){
FileReader in = new FileReader(f1);//object FileReader that refers to f1
int data = in.read(); //read a char at a time
while (data != -1) {
out.write(data);
data = in.read();
}
out.print();
System.out.flush();
in.close();
}
} catch (Exception e) {
System.out.println("Exception: "+e);
}
}
}