目录
BufferedOutPutStream和BufferedInPutStream
ObjectInPutStream和ObjectOutPutStream
文件
创建文件
- File file = new File(String filePath)
- File file = new File(String parentPath,String childPath)
- File file = new File(File parent,String childPath)
@Test public void creat01(){ String filePath = "E:" + File.separator + "io1.txt"; File file = new File(filePath); try { file.createNewFile(); System.out.println("成功"); } catch (IOException e) { e.printStackTrace(); } } //File file = new File(String parentPath,String child) @Test public void creat02(){ String parentPath = "E:" + File.separator; String filePath = "io2.txt"; File file = new File(parentPath,filePath); try { file.createNewFile(); System.out.println("创建成功"); } catch (IOException e) { e.printStackTrace(); } } //File file = new File(File parent,String child) @Test public void creat03(){ File parentFile = new File("E:"); String filepath = "io3.txt"; File file = new File(parentFile,filepath); try { file.createNewFile(); System.out.println("创建成功"); } catch (IOException e) { e.printStackTrace(); } }
在Linux和Java中分隔符不同,可以使用file.separator,不包括:
查看文件属性
public class fileInfromation {
public static void main(String[] args) throws IOException {
File file = new File("E:" + File.separator + "text.txt");
file.createNewFile();
//获得路径
System.out.println(file.getPath());
//获得绝对路径
System.out.println(file.getAbsoluteFile());
//是否是文件
System.out.println(file.isFile());
//是否是目录
System.out.println(file.isDirectory());
//是否存在
System.out.println(file.exists());
//大小
System.out.println(file.length());
//父路径
System.out.println(file.getParent());
}
}
结果
InputStream和outStream
fileinputStream
public class fileInPutStream {
public static void main(String[] args) {
}
@Test
public void inPutStream() throws IOException {
//创建fileInputStream:new FileInputStream(File file),new FileInputStream(String filePath)
//new FileInputStream(FileDescriptor)
//FileInputStream fileInputStream = new FileInputStream("e:\\text.txt");
FileInputStream fileInputStream = new FileInputStream(new File("e:\\text.txt"));
int read = 0;
byte [] a = new byte[8];
//read方法,read()当读到末尾时,返回-1,其他返回读取到的字符
//read(byte[] a)每次读取a.length个数量的值,并将其数值存取到a中,返回值是读取到的数量
//read(byte[] a,int len,int off)从off开始读取len个字符
//应用一,当读取文本文档中不含有中文字符时
// while ((read = fileInputStream.read()) != -1){
// System.out.print((char)read);
// }
//当文本文档中含有中文字符时,使用read(byte[])
while ((read = fileInputStream.read(a))!= -1){
System.out.println(new String(a,0,read));
}
fileInputStream.close();
}
}
fileoutputStream
public class fileOutPutStream
{
public static void main(String[] args) {
}
@Test
public void out() throws IOException {
String string = "hello,world";
//创建与fileINPutStream相同
//创建并写入时,如果文件不存在,文件则会自动创建
//正常写入时是全覆盖,若想在后面添加,添加true即可
FileOutputStream fileOutputStream = new FileOutputStream("e:\\abc.TXT",true);
//write()只能写入int或byte[]
fileOutputStream.write('a');
//string.getBytes可以将字符串转化成字符数组
fileOutputStream.write(string.getBytes(StandardCharsets.UTF_8));
}
}
小应用,将图片拷贝到另一个文件中
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class copy {
public static void main(String[] args) {
}
//将一个图片复制到另一个文件中
@Test
public void copy() throws IOException {
FileInputStream fileInPutStream = null;
FileOutputStream fileOutPutStream = null;
int readlen = 0;
byte [] bytes = new byte[1024];
try {
fileOutPutStream = new FileOutputStream("G:\\copy.gif");
fileInPutStream = new FileInputStream("G:\\test.gif");
//一个一个字符将图片复制
// while ((readlen = fileInPutStream.read()) != -1){
// fileOutPutStream.write(readlen);
// }
//使用字节数组来输入和输出
while ((readlen = fileInPutStream.read(bytes)) != -1){
fileOutPutStream.write(bytes,0,readlen);
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
fileInPutStream.close();
fileOutPutStream.close();
}
}
}
FileReader
FileReader是fileInputStream的子类,创建方法和read方法,使用大致相同
public class fileReader {
public static void main(String[] args) {
}
@Test
public void read(){
//创建方式 FileReader fileReader = new FileReader(file/filepath)
String filepath = "G:\\read.txt";
FileReader fileReader = null;
int read = 0;
char[] chars = new char[10];
try {
fileReader = new FileReader(filepath);
//fileReader.read()方法每次返回int值,为-1读到末尾
// while ((read = fileReader.read()) != -1){
// System.out.print((char)read);
// }
System.out.println("======================================");
//fileReader.read(char() chars),将读到的值给chars,每次返回int,为读取到的字符数量
//当读到结尾时,返回-1
//fileReader.read(char() ,int off,int len) off在数组中开始存的位置,len存取的长度
while ((read = fileReader.read(chars) ) != -1){
System.out.print(new String(chars,0,read));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileWriter
FileWriter每次写入时都要关闭和刷新才能将数据写入
public class fileWriter {
public static void main(String[] args) {
String filePath = "G:\\test.txt";
//FileWriter必须使用flush或close方法才能将数据写入
FileWriter fileWriter = null;
String str = "abcdefg";
char [] chars = str.toCharArray();
try {
fileWriter = new FileWriter(filePath);
//fileWriter.write(int c);字符和int可以转换
fileWriter.write('d');
//fileWriter.write(char[] chars);
fileWriter.write(chars);
//fileWriter.write(char[] charm,int off,int len);
fileWriter.write(chars,0,2);
//fileWriter.write(String);
fileWriter.write("我是傻逼");
//fileWriter.write(String,int off,int len);
fileWriter.write("我是傻逼",2,2);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
bufferedWriter和bufferedReader
包装类,使用上面类可以自由的选择和使用方法,而不用去选择,感觉低级流的大致地位就是外包
用以处理字符文件
public class bufferedWriter {
public static void main(String[] args) {
BufferedWriter bufferedWriter = null;
String filePath = "G:\\cbc.txt";
Scanner scanner = new Scanner(System.in);
try {
bufferedWriter = new BufferedWriter(new FileWriter(filePath));
bufferedWriter.write(scanner.next());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class bufferedReader {
public static void main(String[] args) {
//包装流,创建方式
//BufferRead bufferRead = new BufferRead(Read read)
BufferedReader bufferedReader = null;
String line = null;
String filePath = "G:read.txt";
try {
bufferedReader = new BufferedReader(new FileReader(filePath));
//readline()方法返回字符串,当读到结尾时,返回null
while ((line = bufferedReader.readLine()) != null){
line = bufferedReader.readLine();
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
BufferedOutPutStream和BufferedInPutStream
和上一个大致相同,用于写和读二进制文件
public class bufferCopy {
public static void main(String[] args) throws IOException {
int len = 0;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
byte [] datas = new byte[1024];
bufferedInputStream = new BufferedInputStream(new FileInputStream("G:\\test.gif"));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("D:\\ad.gif"));
while ((len = bufferedInputStream.read(datas,0,1024)) != -1){
bufferedOutputStream.write(datas,0,len);
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
}
ObjectInPutStream和ObjectOutPutStream
将对象传入和读出文件中时,必须按照顺序读出,及序列化
public class objectOuntPutStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//对象流,创建也是包装流
ObjectInputStream objectInPutStream = new ObjectInputStream(new FileInputStream("e:\\load.txt") );
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("e:\\load.txt"));
//写入时的对象需要继承Serializable或者另一个
//写入时会保存数据类型简称序列化
objectOutputStream.writeInt(100);//int会自动转成Integer
objectOutputStream.writeBoolean(true);
objectOutputStream.writeFloat(14.F);
objectOutputStream.writeUTF("安徽理工大学");
objectOutputStream.writeObject(new dog("zzz",1));
objectOutputStream.writeObject(new dog("aaa",1));
//读出时必须按照顺序来读出,否则乱码
System.out.println(objectInPutStream.readInt());
System.out.println(objectInPutStream.readBoolean());
System.out.println(objectInPutStream.readFloat());
System.out.println(objectInPutStream.readUTF());
System.out.println(objectInPutStream.readObject());
System.out.println(objectInPutStream.readObject());
}
}
注意事项