Now I'm learning writing to streams. I'm able to make an error free program but it won't write the chars. here is my code:
import java.io.*;
public class IOHandling{
public static void save() throws IOException{
char[] data = {'h','e','l','l','o'};
File game = new File("hi.txt");
FileWriter iS = new FileWriter(game);
iS.write(data);
}
}My output is a empty file named hi.txt
What am I doing wrong?
Offline
BoltBait is correct but the proper code is:
import java.io.*;
public class IOHandling{
public static void save() throws IOException{
char[] data = {'h','e','l','l','o'};
File game = new File("hi.txt");
FileWriter iS = new FileWriter(game);
iS.write(data);
iS.close();
}
}Offline
You have to close the FileWriter,
than it will work!
import java.io.*;
public class IOTest {
public static void main(String[] args) throws IOException {
FileWriter out = null;
try {
out = new FileWriter(new File("test.txt"));
out.write("Hello World".toCharArray());
} finally {
if (out != null) {
out.close();
}
}
}
}Offline