当前位置:网站首页>IO stream, character read / write, copy
IO stream, character read / write, copy
2022-06-30 04:30:00 【A fat man】
Character reading
package io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.CharBuffer;
/**
* @author : htf
* Character reading
*/
public class TestReader {
// A character One character read
public static void test() {
File file=new File("E:\\temp\\a.txt");
FileReader fr=null;
try {
fr=new FileReader(file);
int c=fr.read();
c=fr.read();
c=fr.read();
c=fr.read();
c=fr.read();
c=fr.read();
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// Traversal read
public static void test1() {
File file=new File("E:\\temp\\a.txt");
FileReader fr=null;
try {
fr=new FileReader(file);
int a=0;
while((a=fr.read())!=-1) {
System.out.println((char)a);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// Enable cache read
public static void test2() {
File file=new File("E:\\temp\\a.txt");
FileReader fr=null;
try {
fr=new FileReader(file);
int a=0;
char[] b=new char[3];// Each read 3 Characters
// Use FileReader Medium read The method parameter is an array , Each read 3 Characters , The return value is -1 when , Read complete
while((a=fr.read(b))!=-1) {
System.out.println(new String(b, 0, a));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//test();
//test1();
test2();
}
}
Character writing Copy
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author : htf
* Character writing and file copying
*
*/
public class TestWriter {
// Read a , Write a inefficiency
public static void test1() {
File src =new File("E:\\temp\\a.txt");
File dest=new File("E:\\temp\\a5.txt");
FileReader fr = null;
FileWriter fw = null;
try {
fr=new FileReader(src);
fw=new FileWriter(dest);
int a=fr.read();
fw.write(a);
a=fr.read();
fw.write(a);
a=fr.read();
fw.write(a);
a=fr.read();
fw.write(a);
a=fr.read();
fw.write(a);
fw.flush();// Refresh
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fw.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// Read a group , Write a group
public static void test2() {
File src =new File("E:\\temp\\a.txt");
File dest=new File("E:\\temp\\a6.txt");
FileReader fr = null;
FileWriter fw = null;
try {
fr=new FileReader(src);
fw=new FileWriter(dest);
char b[]=new char[3];// Read three characters at a time
int a=0;
while(( a=fr.read(b))!=-1) {
fw.write(b,0,a);// Write the data read each time into a6.txt,a, Number of characters written
}
fw.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fw.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
test2();
}
}
边栏推荐
- thinkphp5实现导入功能
- Maya Calendar(POJ1008)
- el-upload上传文件(手动上传,自动上传,上传进度)
- El upload upload file (manual upload, automatic upload, upload progress)
- Differences between beanfactory and factorybean
- MySQL DDL change
- 输入输出及中断技术——微机第六章学习笔记
- Array of small C
- Refers to the difference between IP and *ip at output
- Day 12 advanced programming techniques
猜你喜欢

Error encountered in SQL statement, solve

How to use div boxes to simulate line triangles

Troubleshoot abnormal video playback problems in public network deployment based on Haikang ehomedemo tool

el-upload上傳文件(手動上傳,自動上傳,上傳進度)

lego_ Reading and summary of loam code

Junior students summarize JS basic interview questions

Technology sharing | broadcast function design in integrated dispatching

El upload upload file (manual upload, automatic upload, upload progress)

Myrpc version 6

Interprocess communication
随机推荐
JS deconstruction assignment
Basic knowledge of redis
Blocking queue example
File system and directory operations
Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)
Ananagrams(UVA156)
Titanic(POJ2361)
I spent three years in a big factory outsourcing, which subverted my understanding!
Myrpc version 3
Robot slam navigation core technology and practice Season 1: Chapter 0_ Slam development overview
base64.c
Blue Bridge Cup: magic cube rotation [Vocational group]
FortiGate creates multiple corresponding DDNS dynamic domain names for multiple ADSL interfaces
Anonymous pipeline for interprocess communication
Project safety and quality
Es2019 key summary
Machine learning notes
Fair lock and unfair lock
Mongodb learning
Default value of JS parameter