Java IO API记录

时间:2024-04-29 03:24:54

文件路径:

public static final String FILEPATH= File.separator+"Users"+ File.separator+"xuminzhe"+
File.separator+"Documents"+File.separator+"io";

1.创建文件


public static void main(String[] args) {
File file=new File(Constant.FILEPATH+File.separator+"io.text");
try {
boolean newFile = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

2.查找指定目录下文件


public static void main(String[] args) {
File file=new File(Constant.FILEPATH);
File[] str = file.listFiles();
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}

3.文件流-写入


String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
OutputStream outputStream=new FileOutputStream(file,true);
byte[] bytes = "你好".getBytes();
for (int i = 0; i < bytes.length; i++) {
outputStream.write(bytes[i]);
}
outputStream.close();

4.文件流-读取


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
InputStream inputStream=new FileInputStream(file);
/**
* 单字节读取
*/
{
byte[] bytes = new byte[1024];
int read1;
int count =0;
while((read1 = inputStream.read())!=-1){
bytes[count++]=(byte) read1;
}
System.out.println(new String(bytes));
}
/**
* 多字节读取
*/
{
byte[] bytes=new byte[(int) file.length()];
int read;
while((read=inputStream.read(bytes))!=-1){
System.out.println(new String (bytes));
}
}
}

5.字符流-写入


String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Writer writer=new FileWriter(file,true);
String str="\r\nhello";
writer.write(str);
writer.close();

6.字符流-读取


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Reader read=new FileReader(file);
char[] ch=new char[100];
int temp=0;
int count=0;
while((temp=read.read())!=(-1)){
ch[count++]=(char)temp;
}
read.close();
System.out.println("内容为"+new String(ch,0,count));
}

7.转换流-写入  将输出的字符流转化为字节流


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Writer writer=new java.io.OutputStreamWriter(new FileOutputStream(file,true));
writer.write("kobe");
writer.close();
}

8.转换流-读取 将输入的字节流转换为字符流


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Reader read = new InputStreamReader(new FileInputStream(file));
char[] b=new char[100];
int len=read.read(b);
System.out.println(new String(b,0,len));
read.close();
}

9.对象流


static String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
static File file=new File(filename);
public static void main(String[] args) throws Exception {
serializable(file);
deserializable(file);
}
/**
* 反序列化
* @param file
* @throws IOException
* @throws ClassNotFoundException
*/
private static void deserializable(File file) throws IOException, ClassNotFoundException {
ObjectInputStream stream=new ObjectInputStream(new FileInputStream(file));
Person o = (Person) stream.readObject();
System.out.println(o.toString());
} /**
* 序列化对象
* @param file
* @throws IOException
*/
private static void serializable(File file) throws IOException {
ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream(file,true));
outputStream.writeObject(new Person("xmz",13));
outputStream.close();
}

10.缓冲字符流-读取


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
String line;
while((line=bufferedReader.readLine())!=null){//读取一个文本行
System.out.println(line);
}
bufferedReader.close();
}

11.缓冲字符流-写入


public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
FileWriter fileWriter=new FileWriter(file);
/**
* 为了提高写入的效率,使用了字符流的缓冲区。
* 创建了一个字符写入流的缓冲区对象,并和指定要被缓冲的流对象相关联。
*/
BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
bufferedWriter.write("jordan乔丹");
bufferedWriter.newLine();//换行
bufferedWriter.write("kobe蜗壳");
bufferedWriter.write("wade韦德");
bufferedWriter.flush();
bufferedWriter.close(); }

12 管道流-可用于线程通信


static class Send implements Runnable{
private PipedOutputStream out=null;
public Send() {
out=new PipedOutputStream();
}
public PipedOutputStream getOut(){
return this.out;
}
public void run(){
String message="hello,xmz";
try{
out.write(message.getBytes());
}catch (Exception e) {
e.printStackTrace();
}try{
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}
} /**
* 接受消息类
* */
static class Recive implements Runnable{
private PipedInputStream input=null;
public Recive(){
this.input=new PipedInputStream();
}
public PipedInputStream getInput(){
return this.input;
}
public void run(){
byte[] b=new byte[1000];
int len=0;
try{
len=this.input.read(b);
}catch (Exception e) {
e.printStackTrace();
}try{
input.close();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("接受的内容为 "+(new String(b,0,len)));
}
} public static void main(String[] args) throws IOException {
Send send=new Send();
Recive recive=new Recive();
try{
//管道连接
send.getOut().connect(recive.getInput());
}catch (Exception e) {
e.printStackTrace();
}
new Thread(send).start();
new Thread(recive).start();
}