复习IO
字符输入流读取文件
@Test
public void fileReaderTest01(){
FileReader reader = null;
try {
File file = new File("hello01.txt");
reader = new FileReader(file);
int data;
while ((data = reader.read())!=-1){
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
用字符输入流中重载的read方法读取文件
@Test
public void fileReaderTest02(){
FileReader reader = null;
try {
reader = new FileReader(new File("hello01.txt"));
char[] chars = new char[1024];
int len;
while ((len = reader.read(chars))!=-1){
String str = new String(chars, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用字符输出流写出文件
@Test
public void fileWriterTest01(){
FileWriter writer = null;
try {
File file = new File("hello.txt");
writer = new FileWriter(file);
writer.write("jiagnhu 烽子");
char[] chars = {'江','湖','烽','子','h'};
writer.write(chars);
writer.write(chars,1,3);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用字符输入流字符输出流复制文件
@Test
public void fileReaderfileWriterTest(){
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
File file = new File("hello01.txt");
File file1 = new File("hello03.txt");
fileReader = new FileReader(file);
fileWriter = new FileWriter(file1);
char[] chars = new char[1024];
int len;
while ((len = fileReader.read(chars))!=-1){
System.out.println(new String(chars,0,len));
fileWriter.write(chars,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader!=null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileWriter!=null){
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用字节输入输出流实现对非文本文件的复制
@Test
public void fileInputStreamFileOutputStreamtest(){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
File file = new File("2021-07-17_104603.jpg");
File file1 = new File("第三次复制的图片.jpg");
fileInputStream = new FileInputStream(file);
fileOutputStream = new FileOutputStream(file1);
byte[] bytes = new byte[1024];
int len;
while ((len = fileInputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream!=null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用缓冲字符流读取文本文件
@Test
public void BufferedReaderTest(){
BufferedReader bufferedReader = null;
try {
File file = new File("hello01.txt");
FileReader reader = new FileReader(file);
bufferedReader = new BufferedReader(reader);
String str;
while ((str = bufferedReader.readLine())!=null){
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader!=null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用字符输出流输出
@Test
public void BufferedWriterTest(){
BufferedWriter bufferedWriter = null;
try {
File file = new File("hello.txt");
FileWriter fileWriter = new FileWriter(file, true);
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("江湖烽子");
char[] chars = {'江', '湖', '烽', '子'};
bufferedWriter.write(chars);
bufferedWriter.write(chars,1,2);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedWriter!=null){
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用缓冲字符流复制文件
@Test
public void BufferedReaderBufferWriterTest(){
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
File file = new File("hello01.txt");
File file1 = new File("hello04.txt");
FileReader fileReader = new FileReader(file);
FileWriter fileWriter = new FileWriter(file1);
bufferedReader = new BufferedReader(fileReader);
bufferedWriter = new BufferedWriter(fileWriter);
String str ;
while ((str = bufferedReader.readLine())!=null){
System.out.println(str);
bufferedWriter.write(str+"\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader!=null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedWriter!=null){
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用缓冲字节流复制非文本文件
@Test
public void BufferedInputStreamBufferedOutputStreamTest(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File file = new File("2021-07-17_104603.jpg");
File file1 = new File("第四次复制的图片.jpg");
FileInputStream fileInputStream = new FileInputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream(file1);
bis = new BufferedInputStream(fileInputStream);
bos = new BufferedOutputStream(fileOutputStream);
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
获取文本上每个字符出现的次数
@Test
public void test(){
FileReader reader = null;
FileWriter writer = null;
try {
reader = new FileReader("hello.txt");
writer = new FileWriter("hi.txt");
HashMap<Character, Integer> map = new HashMap<>();
int len ;
while ((len = reader.read())!=-1){
char ch = (char) len;
if (map.get(ch)==null){
map.put(ch,1);
}else {
map.put(ch,map.get(ch)+1);
}
}
Set<Map.Entry<Character, Integer>> entries = map.entrySet();
for (Map.Entry<Character, Integer> entry : entries) {
switch (entry.getKey()){
case ' ':
writer.write("空格="+entry.getValue());
break;
case '\t':
writer.write("Tab键="+entry.getValue());
break;
case '\n':
writer.write("换行="+entry.getValue());
break;
case '\r':
writer.write("回车="+entry.getValue());
break;
default:
writer.write(entry+"");
}
writer.write("\n");
System.out.println(entry);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}