Use the FileOutputStream stream to write data to a file
Import java.io.*;p ublic class Testfileoutputstream {public static void main (String args[]) {int b = 0; FileInputStream in = null; FileOutputStream out = null; try {in = Newfileinputstream ("D:/restudyjava/restudy3/testfileinputstream.java"); out = new FileOutputStream ("D:/restudyjava/restudy3/testfileoutputstream1.java"); Indicates the file to write data to, and if a file such as Testfileoutputstream1.java does not exist in the specified path, then the system automatically creates a while ((b = in.read ()) =-1) { Out.write (b); Call the Write (int c) method to write all the characters read to the specified file} in.close (); Out.close (); } catch (FileNotFoundException e) {System.out.println ("file read failed"); System.exit (-1);//non-normal exit} catch (IOException E1) {System.out.println ("File copy failed! "); System.exit (-1); } System.out. println ("The contents of the Testfileinputstream.java file have been successfully copied into the file Testfileoutstream1.java"); }}
<textarea spellcheck="false" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none" tabindex="0" readonly=""></textarea>28 1
2
Import java.io.*;
3
4
{
5
Public static void Main (String args[]) {
6
int b = 0;
7
FileInputStream in = null;
8
FileOutputStream out = null;
9
try {
10
In = Newfileinputstream ("D:/restudyjava/restudy3/testfileinputstream.java");
11
out = new FileOutputStream ("D:/restudyjava/restudy3/testfileoutputstream1.java");
12
//Indicates the file to be written to, and if a file such as Testfileoutputstream1.java does not exist in the specified path, the system automatically creates a
13
While ((b = In.read ())! =-1) {
14
Out.write (b);
15
//Call the Write (int c) method to write all the characters read to the specified file.
16
}
17
In.close ();
18
Out.close ();
19
} catch (FileNotFoundException e) {
20
System.out.println ("file read failed");
21st
System.exit (-1);//Abnormal exit
22
} catch (IOException E1) {
23
System.out.println ("File copy failed! ");
24
System.exit (-1);
25
}
26
System.out. println ("The contents of the Testfileinputstream.java file have been successfully copied into the file Testfileoutstream1.java");
27
}
28
}
The
saves several double types of data in the A.txt file, reads the data, and then saves the sort to b.txt