File input and output streams in Java and java input and output streams
I recently learned how to read the output file and paste some code:
TestInputStream1 ():
String strFile = "Data/custom roup. cpp ";
File = new File (strFile );
InputStream in = null;
In =NewFileInputStream (file );
// Create an array of suitable file sizes to read data from the file at a time
B1=New Byte[(Int) File. length ()];
// Read the content of the file to the B [] array. If read () returns the bytes read,
// If the content is empty,-1 is returned, which can be used to determine whether the file content has been read.
In. read (B1);
In. close ();
TextArea. append (new String (b1 ));
TestInputStream2 ():
Int iSize = 1024;
Boolean blSleep = true;
String strFile = "Data/custom roup. cpp ";
File file =NewFile (strFile );
Int iCount = (int) file. length ()/iSize;
InputStream in = null;
In = new FileInputStream (file );
B2 = new byte [iSize];
IntTemp = 0;
IntNo = 0;
While (temp = in. read ())! =-1 ){
No ++;
In. read (b2 );
System. out. println (new String (b2 ));
If (no <iCount ){
In. read (b2 );
System. out. println (new String (b2 ));
}
Else {
BlSleep = false;
System. out. print (char) temp );
}
}
In. close ();
Run TestInputStream1 ()After:
TestOutputStream ():(Read the text file, display it to the component, and write it to another file)
// Variables related to file writing
String strOut = "* Data/repeated roup_copy.cpp ";
File file =NewFile (strOut );
OutputStream output =Null;
Output =NewFileOutputStream (file );
Output. write (B1);
Output. close ();
TestPrintStream ()://Read and output the screen and output it to the file.
String strFile = "Data/custom roup. cpp ";
File file = new File (strFile );
InputStream in = null;
In = new FileInputStream (file );
// Create an array of suitable file sizes to read data from the file at a time
B1 = new byte [(int) file. length ()];
// Read the content of the file to the B [] array. If read () returns the bytes read,
// If the content is empty,-1 is returned, which can be used to determine whether the file content has been read.
In. read (b1 );
In. close ();
// Output data to the screen
PrintStream printStream = new PrintStream (System. out );
PrintStream. println (new String (b1 ));
PrintStream. flush (); // complete the output and clear the buffer.
PrintStream. close ();
// Output data to a file
File filePrint = new File ("Data/print. cpp ");
PrintStream = new PrintStream (filePrint );
PrintStream. println (new String (b1 ));
PrintStream. close ();
TestDataInputStream ():
// Keyboard input (how to allocate space based on the amount of data input by the user)
Byte B [] = new byte [12];
DataInputStream dataInputStream = new DataInputStream (System. in );
String str = null;
DataInputStream. read (B );
DataInputStream. close ();
System. out. println (new String (B ));
// File input
String strFile = "Data/table 2-5.txt ";
Byte byFile [] = null;
File file = new File (strFile );
InputStream instream = null;
Instream = new FileInputStream (file );
DataInputStream = new DataInputStream (instream );
Float temp = (float) 0.0;
Str = null;
ByFile = new byte [(int) file. length ()];
DataInputStream. read (byFile );
DataInputStream. close ();
System. out. println (new String (byFile ));
TestDataInputStream2 ()://Binary
System.Out. Println ("reading a value from a file each time ");
DataInputStream dataInputStream =Null;
// File input
String strFile = "Data/table 2-5_copy.txt ";
File file =NewFile (strFile );
InputStream instream =Null;
Instream =NewFileInputStream (file );
DataInputStream =NewDataInputStream (instream );
System.Out. Println ("output of the binary file read result! ");
FloatTemp = (Float0.0;
// Determine whether the file ends
While(DataInputStream. available ()> 0 ){
Temp = dataInputStream. readFloat ();
System.Out. Println (temp );
Instream. close ();
DataInputStream. close ();
TestDataOutputStream ():
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
// File input
Byte byFile [] = null;
File file = new File ("Data/table 2-5.txt ");
InputStream instream = null;
Instream = new FileInputStream (file );
InputStream = new DataInputStream (instream );
String str = null;
ByFile = new byte [(int) file. length ()];
InputStream. read (byFile );
Str = new String (byFile );
InputStream. close ();
String strSplit [] = str. split ("");
System. out. println (strSplit. length );
// Write the data in the string 'str' into the file one by one (binary format file)
String strFile = "Data/table 2-5_copy.txt ";
File = new File (strFile );
OutputStream out = null;
Out = new FileOutputStream (file );
OutputStream = new DataOutputStream (out );
For (int I = 0; I <strSplit. length; I ++ ){
Float temp = Float. valueOf (strSplit [I]);
System. out. println (temp );
OutputStream. writeFloat (temp );
}
Out. close ();
OutputStream. close ();
}
TestBufferReaderAndBufferedWriter ():
// Keyboard input
InputStreamReader isr =NewInputStreamReader (System.In);
BufferedReader br =NewBufferedReader (isr );
String str =Null;
Str = br. readLine ();
Isr. close ();
Br. close ();
System.Out. Println (str );
// Read the file
Str =Null;
String strFile = "Data/result_table 2-5.txt ";
If(Isr! =Null) Isr =Null;
Isr =NewInputStreamReader (NewFileInputStream (NewFile (strFile )));
Br =NewBufferedReader (isr );
// Write an object
String strWrite = "Data/result_table 2-5_copy.txt ";
OutputStreamWriter out =Null;
BufferedWriter bw =Null;
Out =NewOutputStreamWriter (NewFileOutputStream (NewFile (strWrite )));
Bw =NewBufferedWriter (out );
// Is there a rewind function in Java that allows you to relocate pointers ?????????
Isr =NewInputStreamReader (NewFileInputStream (NewFile (strFile )));
Br =NewBufferedReader (isr );
Str =Null;
While(Str = br. readLine ())! =Null){
System.Out. Println (str );
Bw. write (str); // when readLine is used to read data, the linefeed has been canceled.
Bw. newLine ();
}
Bw. close ();
Out. close ();