Java-read data from files and java reads data
1. FileInputStream ()
// Construct a byte input stream object. The parameter is the file name FileInputStream fin = new FileInputStream ("message"); System. out. println ("number of bytes that can be read from the input stream:" + fin. available (); // create byte array B. The data read from the input stream is temporarily stored in byte B [] = new byte [fin. available ()]; // read data from the input stream fin. read (B); String str5 = new String (B); System. out. println (str5); // close the input stream. For file operations, you must close the input/output stream fin at the end. close ();
2. RandomAccessFile ()
String str6=""; ArrayList<String> strs=new ArrayList<String>(); RandomAccessFile file=new RandomAccessFile("message","r"); str6 = file.readLine(); while(str6!=null){ strs.add(str6); str6 = file.readLine(); } for(int j=0;j<strs.size();j++){ System.out.println(strs.get(j)); } file.close();
3. File
File file2 = new File ("message"); if (file2.exists () & file2.isFile () {InputStream is = new FileInputStream (file2 ); byte [] buf = new byte [is. available ()]; System. out. println (is. available (); while (is. read (buf )! =-1) {// print System. out. println (new String (buf) each read ));}}
4. Avoid garbled characters
File file3 = new File ("message"); if (file3.exists () & file3.isFile () {try {InputStream is = new FileInputStream (file3 ); inputStreamReader reader = new InputStreamReader (is, "UTF-8"); // char [] cbuf = new char [is. available ()]; // character, byte StringBuffer sb2 = new StringBuffer (); // 2 while (reader. read (cbuf )! =-1) {sb2.append (cbuf);} System. out. println (sb2.toString ();} catch (Exception e ){}}
5. BufferedReader
File file = new File("message"); try { InputStream is = new FileInputStream(file); if (file.exists() && file.isFile()) { BufferedReader br = new BufferedReader( new InputStreamReader(is, "utf-8")); StringBuffer sb2 = new StringBuffer(); String line = null; while ((line = br.readLine()) != null) { sb2.append(line + "\n"); } br.close(); System.out.println(sb2.toString()); } } catch (Exception e) { }
The best code for writing files and reading data from files in Java!
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileReader;
Import java. io. FileWriter;
Import java. io. IOException;
Public class IOTest {
Public static void main (String [] args ){
String str = "123 \ r \ n456 ";
WriteFile (str); // write
String str1 = readFile (); // read
System. out. println (str1 );
}
/**
* Pass written content
* @ Param str
*/
Static void writeFile (String str ){
Try {
File file = new File ("d: \ file.txt ");
If (file. exists () {// exists
File. delete (); // delete and recreate
File. createNewFile ();
} Else {
File. createNewFile (); // directly created if the file does not exist
}
FileWriter fw = new FileWriter (file); // file write IO
Fw. write (str );
Fw. flush ();
Fw. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
/**
* Returned read content
* @ Return
*/
Static String readFile (){
String str = "", temp = null;
Try {
File file = new File ("d: \ file.txt ");
FileReader fr = new FileReader (file );
BufferedReader br = new BufferedReader (fr); // File Read IO
While (temp = br. readLine ())! = Null) {// until the end of reading
Str + = (temp + "\ n ");
}
Br. close ();
Fr. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return str;
}
}
I just wrote it. It's a good time for friends to study it.
Read more API and practice more... the rest is full>
How to read data from a file in java
It can be divided into two types: Read byte and read character.
◎ FileInputStream byte input stream Read File ◎
Public class Maintest {
Public static void main (String [] args) throws IOException {
File f = new File ("G: \ just for fun \ xiangwei.txt ");
FileInputStream fin = new FileInputStream (f );
Byte [] bs = new byte [1024];
Int count = 0;
While (count = fin. read (bs)> 0)
{
String str = new String (bs, 0, count); // define new variables repeatedly: each time a new variable is redefined, it receives new read data.
System. out. println (str); // output new variables repeatedly: each time a new variable is output
}
Fin. close ();
}
}
◎ FileReader character input stream Read File ◎
Public class Maintest {
Public static void main (String [] args) throws IOException {
File f = new File ("H: \ just for fun \ xiangwei.txt ");
FileReader fre = new FileReader (f );
BufferedReader bre = new BufferedReader (fre );
String str = "";
While (str = bre. readLine ())! = Null) // ● determines whether the last row does not exist. It is null.
{
System. out. println (str );
}
Bre. close ();
Fre. close ();
}
}