java IO详解—流
1.IO流
这里可以看宜春的解释说明
2.思维导图
3.代码
1.输入流
package com.xinyisu.study;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class T01 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream is = new FileInputStream("D://assignment//大学作业//大四 上//课程//1.txt");
//t是ascii码值
int t = is.read();
//强制转换为char类型
System.out.println((char)t);
t = is.read();
System.out.println((char)t);
byte[] bytes = new byte[8];
int count = is.read(bytes);
//字节数组转换为字符串
System.out.println(new String(bytes));
is.close();
}
}
2.输入流的学习
package com.xinyisu.study;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class T02 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream is = new FileInputStream("D://assignment//大学作业//大四 上//课程//1.txt");
byte[] bytes = new byte[8];
int len = -1;
while((len = is.read(bytes)) != -1);
{
String temp = new String(bytes ,0 ,len);
System.out.print(temp);
}
is.close();
}
}
3.输入流,增强流,缓冲池
package com.xinyisu.study;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.io.InputStream;
public class T03 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream is = new FileInputStream("D://assignment//1.txt");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//增强流,方向必须得一致
BufferedInputStream bis = new BufferedInputStream(is);
byte[] bytes = new byte[8];
int len = -1;
while((len = bis.read(bytes)) != -1);
{
baos.write(bytes,0,len);
}
//先关后开的
bis.close();
is.close();
byte[] result = baos.toByteArray();
System.out.println(new String(result));
}
}
4.输出流
package com.xinyisu.study;
import java.io.*;
public class T04 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
OutputStream os = new FileOutputStream("D://assignment//2.txt",true);
os.write("哈哈哈".getBytes());
//回车 换行
os.write(13);
os.write(10);
os.write("哈哈哈".getBytes());
os.flush();
os.close();
}
}
5.file的学习
package com.xinyisu.study;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
//file不对文件内容操作,对文件内容操作
//对文件内容操作,我们用流
//该类可以得到文件的很多属性,删除文件,创建文件,判断文件是否存在
public class T05 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub OutputStream os = new FileOutputStream("D://assignment//2.txt",true);
File file = new File("D://assignment//2.txt");
//判断文件是否存在
System.out.println(file.exists());
//判断文件的权限
System.out.println(file.canExecute());
Date d= new Date();
long time = file.lastModified();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String temp = sdf.format(d);
System.out.println(temp);
file.deleteOnExit();
file.createNewFile();
}
}
6.吧D盘的文件转移到E盘去
package com.xinyisu.study;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class T06 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("D://assignment//2.txt");
FileWriter fw = new FileWriter("E://2.txt");
int ch=0;
while((ch=fr.read())!=-1)
{
fw.write(ch);
}
fr.close();
fw.close();
}
}