java IO流学习笔记二

/**
*
*
* 目录分隔符用\\或/
*
* @time 下午11:42:08
* @author retacn yue
* @Email [email protected]
*/
public class Demo1 {


public static void main(String[] args) {
// writeFile();
// readFile();
// readFile2();
// readFile3();
// readFile4();
copyFile();
}


/************** 字符流 **********************************************************/
/**
* 读取一个文件内容
*/
public static void readFile() {
FileReader fileReader = null;
try {
fileReader = new FileReader("a.txt");
int ch = 0;
while ((ch = fileReader.read()) != -1) {
System.out.println((char) ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fileReader) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


}


/**
* 读取方式二
*/
public static void readFile2() {
FileReader fileReader = null;
try {
fileReader = new FileReader("a.txt");
char[] buff = new char[1024];
int len = 0;
while ((len = fileReader.read(buff)) != -1) {
System.out.println(new String(buff, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fileReader) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


}


/**
* 读取方式三
*/
public static void readFile3() {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
fileReader = new FileReader("a.txt");
bufferedReader = new BufferedReader(fileReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fileReader) {
fileReader.close();
}
if (null != bufferedReader) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


}


/**
* 这个面试过
*
* 读取方式四
*/
public static void readFile4() {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
List<String> list = new ArrayList<String>();
fileReader = new FileReader("a.txt");
bufferedReader = new BufferedReader(fileReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
list.add(line);
}
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String string = (String) iterator.next();
System.out.println(string);


}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fileReader) {
fileReader.close();
}
if (null != bufferedReader) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}


}


/**
* 文本数据存储到一个文件中
*/
public static void writeFile() {
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter("b.txt");
fileWriter.write("岳振华");
// 刷新
// fileWriter.flush();
// 关闭此流,但要先刷新它


} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileWriter != null) {
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


/**
* 文件的拷贝
*/
public static void copyFile() {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader("a.txt"));
bufferedWriter = new BufferedWriter(new FileWriter("b.txt"));
String line = null;
// readLine 在读到回车换行时将数据一次性返回
while ((line = bufferedReader.readLine()) != null) {
bufferedWriter.write(line);
// 添加一个加车换行
bufferedWriter.newLine();
bufferedWriter.flush();
}


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != bufferedReader) {
bufferedReader.close();
}
if (null != bufferedWriter) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/******************** 字节流 ********************************************/
}

你可能感兴趣的:(java io)