@[TOC](java文件流相关做操)
# java文件流操作
1 字节流
public static void main(String[] args) {
InputStream in = null;
OutputStream out = null;
try {
//得到输入流
in = new FileInputStream("E:\\test\\a.txt");
//得到输出流
File file = new File("E:\\test\\b.txt");
if (!file.exists()) {
file.createNewFile();
}
out = new FileOutputStream(file, true);
int i;//从输入流读取一定数量的字节,返回 0 到 255 范围内的 int 型字节值
while ((i = in.read()) != -1) {
out.write(i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.字符流
public class FileCharCopy {
public static void main(String[] args) {
FileCharCopy f = new FileCharCopy();
try {
f.copy("d:/File/1.txt","d:/CopyFile/1.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
public void copy(String f1,String f2) throws Exception{
FileReader fr = new FileReader(f1);
FileWriter fw =new FileWriter(f2,false);
//int value=fr.read();
//while(value!=-1){
//fw.write(value);
//fw.flush();
//value=fr.read();
//}
char [] chars = new char[1024];
int len =fr.read(chars);
while(len!=-1){
fw.write(chars, 0, len);
fw.flush();
len =fr.read(chars);
}
fr.close();
fw.close();
}
3字符缓冲流
public class Test {
// 定义文件路径
File f = new File("F:\\test.txt");
//字符缓冲流读取的方法
public String writeInFile() throws IOException{
String str = "";
String count = "";
try {
// 使用字符流对文件进行读取
BufferedReader bf = new BufferedReader(new FileReader(f));
while (true) {
//读取每一行数据并将其赋值给str
if ((count = bf.readLine()) != null) {
str += count;
} else {
break;
}
}
// 关闭流
bf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return str;
}
//字符流写入方法
public void getReader(){
try {
//其中true表示在原本文件内容的尾部添加,若不写则表示清空文件后再添加内容
PrintWriter pw=new PrintWriter(new FileWriter(f,true));
pw.write("测试输入字符串到文件中2");
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
Test test=new Test();
//将字符串输入到文件中
test.getReader();
//读取相对应的字符串
String str=test.writeInFile();
//将文件中内容在控制台输出
System.out.println("文件内容为:"+str);
}
}
4.property读写
String path = "单据号生成调用.txt";
File file1 = new File(path);
//获取全路径
String canonicalPath = file1.getCanonicalPath();
//通过Files获取文件的输出流
OutputStream fos = Files.newOutputStream(Paths.get(canonicalPath));
//设置属性值
Properties prop = new Properties();
prop.setProperty("id", "123");
prop.setProperty("name", "张三");
//将此 Properties 表中的属性列表(键和元素对)写入输出流,将设置的属性值通过输出流保存到文件中
prop.store(fos, null);
//通过Files获取文件输入流
InputStream ins = Files.newInputStream(Paths.get(canonicalPath));
//从输入流中读取属性列表
prop.load(ins);
String id = prop.getProperty("id");
String name = prop.getProperty("name");
System.out.println(id);
System.out.println(name);
5.便捷读取
//按字节读取文件 jdk 1.7
byte[] contentBytes = Files.readAllBytes(Paths.get("source.txt"));
Files.write(Paths.get("target.txt"), contentBytes,
StandardOpenOption.CREATE);
//jdk 1.7
List<String> readAllLines = Files.readAllLines(Paths.get("source.txt"));
Files.write(Paths.get("target.txt"), readAllLines,
Charset.defaultCharset(), StandardOpenOption.CREATE);
//按行读取文件 1.8
List<String> contentLines = Files.lines(Paths.get("source.txt")).collect(java.util.stream.Collectors.toList());
// fileContent.forEach(o->{System.out.println(o);});
Files.write(Paths.get("target.txt"), contentLines,
Charset.defaultCharset(), StandardOpenOption.CREATE);
6.获取文件大小方法
// 获取文件大小的三种方法
File file = new File("/Users/zhaobin/Downloads/zoc7213.dmg");
long length2 = file.length();
System.err.println(length2);
// 文件过大时不精确
FileInputStream is = new FileInputStream(file);
int available = is.available();
System.out.println(available);
FileChannel channel = is.getChannel();
long size = channel.size();
System.out.println(size);
is.close();