1. 对于文本文件的读写
1.1. 读取文本文件
package com.martinwangjun.io;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main (String[] args) throws IOException
{
FileReader fileReader = new FileReader(
"./src/com/martinwangjun/io/FileReaderTest.java" );
char [] buffer = new char [1024 ];
int hasReader = 0 ;
while ((hasReader = fileReader.read(buffer)) > 0 ) {
System.out.println(new String(buffer, 0 , hasReader));
}
fileReader.close();
}
}
1.2. 写入文本文件
package com.martinwangjun.io;
import java.io.FileWriter;
public class FileWriterTest {
public static void main (String[] args) {
try (FileWriter fileWriter = new FileWriter(
"src/com/martinwangjun/io/poem.txt" )) {
fileWriter.write("先令\r\n" );
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
2. 二进制文件
package com.martinwangjun.io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamTest {
public static void main (String[] args) throws IOException{
try (FileInputStream fileInputStream = new FileInputStream(
"src/com/martinwangjun/io/01.jpg" );
FileOutputStream fileOutputStream = new FileOutputStream(
"src/com/martinwangjun/io/02.jpg" );
){
byte [] buffer = new byte [1024 ];
int hasRead = 0 ;
while ((hasRead = fileInputStream.read(buffer)) > 0 ) {
fileOutputStream.write(buffer, 0 , hasRead);
}
buffer = "Powered By Martin" .getBytes();
fileOutputStream.write(buffer, 0 ,
"Powered By Martin" .length());
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
3. 利用包装流
3.1. 利用PrintStream流来包装FileOutputStream
package com.martinwangjun.io;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class PrintStreamTest {
public static void main (String[] args) throws IOException {
try (
FileOutputStream fos = new FileOutputStream(
"src/com/martinwangjun/io/test.txt" );
PrintStream ps = new PrintStream(fos))
{
ps.println("普通字符串" );
ps.println(new PrintStreamTest());
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
4. 获取用户输入
package com.martinwangjun.io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class KeyinTest {
public static void main (String[] args) throws IOException{
InputStreamReader reader = null ;
BufferedReader bufferedReader = null ;
try {
reader = new InputStreamReader(System.in);
bufferedReader = new BufferedReader(reader);
String line = null ;
while ((line = bufferedReader.readLine()) != null ) {
if (line.toLowerCase().equals("exit" )){
System.exit(1 );
}
System.out.println(line);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (reader != null ){
try {
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (bufferedReader != null ){
try {
bufferedReader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
5. 添加而不是覆盖
package com.martinwangjun.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class BufferedWriteTextFile {
public static void main (String[] args) throws Exception {
try (
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(
"src/com/martinwangjun/io/output.txt" , true )));
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in)))
{
String line = null ;
while ( ! (line = bufferedReader.readLine()).equals("" )){
bufferedWriter.write(line);
bufferedWriter.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}