Code
---------
import java.io.FileOutputStream;
class FileWrite {
public static void main(String args[]) throws Exception {
String st = "I am going to be written in the file.";
// Way-1
FileOutputStream fout = new FileOutputStream("output.txt");
byte[] b = st.getBytes();
fout.write(b);
fout.close();
// Way-2
FileOutputStream fout1 = new FileOutputStream("output1.txt");
// Convert string to char array
char[] c = st.toCharArray();
for (int i = 0; i < c.length; i++) {
fout1.write(c[i]);
}
fout1.close();
// Way-3
FileOutputStream fout2 = new FileOutputStream("output2.txt");
byte[] b1 = st.getBytes();
fout2.write(b1, 0, b1.length);
fout2.close();
}
}
---------
import java.io.FileOutputStream;
class FileWrite {
public static void main(String args[]) throws Exception {
String st = "I am going to be written in the file.";
// Way-1
FileOutputStream fout = new FileOutputStream("output.txt");
byte[] b = st.getBytes();
fout.write(b);
fout.close();
// Way-2
FileOutputStream fout1 = new FileOutputStream("output1.txt");
// Convert string to char array
char[] c = st.toCharArray();
for (int i = 0; i < c.length; i++) {
fout1.write(c[i]);
}
fout1.close();
// Way-3
FileOutputStream fout2 = new FileOutputStream("output2.txt");
byte[] b1 = st.getBytes();
fout2.write(b1, 0, b1.length);
fout2.close();
}
}
No comments:
Post a Comment