学习 资源1
学习资源 2
1、文件字节流
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
//try-with-resource语法,自动close
try(FileInputStream file = new FileInputStream("E:/text.txt")) {
System.out.println("");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
读入
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
//try-with-resource语法,自动close
try(FileInputStream file = new FileInputStream("E:/text.txt")) {
int i;
while((i=file.read())!=-1) { //读一个字符,没有返回-1
System.out.print((char) i);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
//try-with-resource语法,自动close
try(FileInputStream file = new FileInputStream("E:/text.txt")) {
byte[] b = new byte[3];
while(file.read(b)!=-1) //一次读入三个字符
{
System.out.println(new String(b));
}
// Hel
// lo
// Wor
// ldr
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
//try-with-resource语法,自动close
try(FileInputStream file = new FileInputStream("E:/text.txt")) {
byte[] b = new byte[file.available()];//返回有多少个字节可读
file.read(b);
System.out.println(new String(b));
//Hello World
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
输出
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
//覆盖文件原有内容
try(FileOutputStream file = new FileOutputStream("E:/text.txt")) {
file.write("hello".getBytes());//字符串转为byte数组才行
file.write("world".getBytes(),2,3);
//hellorld;
file.flush(); //刷新
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
//在文件后面接着写
try(FileOutputStream file = new FileOutputStream("E:/text.txt",true)) {
file.write("hello".getBytes());//字符串转为byte数组才行
file.write("world".getBytes(),2,3);
//hellorldhellorld
file.flush(); //刷新
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
文件拷贝
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try(FileOutputStream outputStream = new FileOutputStream("E:/textout.txt");
FileInputStream inputStream = new FileInputStream("E:/text.txt")) {
byte[] bytes = new byte[10]; //使用长度为10的byte[]做传输媒介
int tmp; //存储本地读取字节数
while ((tmp = inputStream.read(bytes)) != -1) { //直到读取完成为止
outputStream.write(bytes, 0, tmp);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
2、文件字符流
读纯文本文件,一次读一个字符,中英文均可
读入
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try(FileReader reader = new FileReader(new File("src/in.txt"))) {
System.out.println(reader.read());//你
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
输出
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try(FileWriter writer = new FileWriter("src/our.txt");) {
writer.write("Hello World");//不用转为byte数组
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
拷贝文件
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try(FileWriter writer = new FileWriter("src/out.txt");
FileReader reader = new FileReader("src/in.txt")) {
char[] c=new char[100];
int len;
while((len=reader.read(c))!=-1)
writer.write(c,0,len);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
3、文件对象
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
File file=new File("src/in.txt");
System.out.println(file.exists());//true
System.out.println(file.isFile());//true
System.out.println(file.getAbsolutePath());//该文件的绝对路径
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
File file=new File("filetest");
file.mkdir();//创建文件夹
file=new File("filetest/test.txt");
file.createNewFile();//创建文件
}
}
文件拷贝+进度条
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
File file=new File("src/in.txt");
try(FileInputStream in=new FileInputStream(file);
FileOutputStream out=new FileOutputStream("src/out.txt")) {
byte[] by=new byte[20];
long len=file.length();
long sum=0; int chalen;
while((chalen=in.read(by))!=-1)
{
sum+=chalen;
out.write(by,0,chalen);
System.out.println("已拷贝:"+(double)sum/len*100+"%");
// 已拷贝:12.658227848101266%
// 已拷贝:25.31645569620253%
// 已拷贝:37.9746835443038%
// 已拷贝:50.63291139240506%
// 已拷贝:63.29113924050633%
// 已拷贝:75.9493670886076%
// 已拷贝:88.60759493670885%
// 已拷贝:100.0%
}
}catch (Exception e)
{
e.getStackTrace();
}
}
}
4、缓冲流
缓冲字节流
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try(BufferedInputStream in=new BufferedInputStream(new FileInputStream("src/in.txt"));)
{
in.mark(0);
System.out.print((char)in.read());
System.out.print((char)in.read());
System.out.println((char)in.read());//hel
in.reset(); //从mark处开始读
System.out.print((char)in.read());
System.out.print((char)in.read());
System.out.print((char)in.read());//hel
}
catch(Exception e)
{
e.getStackTrace();
}
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try(BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("src/out.txt"));)
{
out.write("Hello World!".getBytes());;
}
catch(Exception e)
{
e.getStackTrace();
}
}
}
缓冲字符流
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try(BufferedReader in=new BufferedReader(new FileReader("src/in.txt"));)
{
System.out.println(in.readLine());//helloworld
}
catch(Exception e)
{
e.getStackTrace();
}
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try(BufferedWriter out=new BufferedWriter(new FileWriter("src/out.txt"));)
{
out.write("hello");
out.newLine();
out.write("world");
// hello
// world
}
catch(Exception e)
{
e.getStackTrace();
}
}
}
5、转换流
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try(OutputStreamWriter out=new OutputStreamWriter(new FileOutputStream("src/out.txt"));)
{
out.write("你好");
}
catch(Exception e)
{
e.getStackTrace();
}
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try(InputStreamReader in=new InputStreamReader(new FileInputStream("src/in.txt"));)
{
System.out.println((char)in.read());//你
System.out.println((char)in.read());//好
}
catch(Exception e)
{
e.getStackTrace();
}
}
}
6、打印流
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (PrintStream out = new PrintStream(new FileOutputStream("src/out.txt"));) {
out.println("Hello World!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
7、数据流和对象流
数据流(使用情况少,了解)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (DataInputStream in = new DataInputStream(new FileInputStream("src/in.txt"));) {
System.out.println((char)in.readByte());//2
} catch (Exception e) {
e.printStackTrace();
}
}
}
对象流
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("src/out.txt"));
ObjectInputStream in = new ObjectInputStream(new FileInputStream("src/out.txt"))) {
List<String> list=new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
out.writeObject(list);
//�� sr java.util.ArrayListx����a� I sizexp w t At Bt Ct Dt Ex
Object o=in.readObject();
System.out.println(o);
// [A, B, C, D, E]
} catch (Exception e) {
e.printStackTrace();
}
}
}
对于自已定义的类需要实现序列化才能写读
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("src/out.txt"));
ObjectInputStream in = new ObjectInputStream(new FileInputStream("src/out.txt"))) {
Person person = new Person();
person.name= "zhangsan";
out.writeObject(person);
Person person1 = (Person) in.readObject();
System.out.println(person1.name);//zhangsan
} catch (Exception e) {
e.printStackTrace();
}
}
static class Person implements Serializable {
String name;
//transient 忽略该变量
}
}