1、什么是流?
流可以理解为一种传输数据的通道,I/O流(即Input/Output,输入/输出),java中所有输入流都是抽象类 InputStream(字节输入流) 或抽象类 Reader(字符输入流) 的子类;而所有输出流都是抽象类OutputStream(字节输出流) 或抽象类Writer(字符输出流) 的子类。
2、输入流
InputStream类是字节输入流的抽象类,是所有字节输入流的父类。java中的字符是Unicode编码,是双字节的。InputStream是用来处理字节的,不适合处理字符文本。所以java有单独的类Reader来处理字符,但Reader类并没有替换InputStream类。
3、输出流
同理,OutputStream类是字节输入流的抽象类,是所有字节输入流的父类。java中的字符是Unicode编码,是双字节的。OutputStream是用来处理字节的,不适合处理字符文本。所以java有单独的类Writer来处理字符,但Writer类并没有替换OutputStream类。
4、File类
File类是java.io包中唯一代表磁盘文件本身的对象。File类定义了一些方法来操作文件,可以通过调用File类中的方法,实现创建、删除、重命名文件等操作。数据流可以将数据写入到文件中,文件也是数据流最常用的数据媒体。
1、文件删除和创建
创建的三种方式:
- File(String pathname)
new File(String pathname)
其中pathname指路径名称
File file = new File(“d:/1.txt”); - File(String parent,String child)
根据父路径和子路径字符串(包含文件名)创建一个新的File对象。 - File(File f,String child)
new File(File f,String child)
2、获取文件信息
方法 | 返回值 | 说明 |
---|---|---|
getName | String | 获取文件名称 |
canRead() | boolean | 判断文件是否可读 |
canWrite() | boolean | 判断文件是否可被写入 |
exists() | boolean | 判断文件是否存在 |
length() | long | 获取文件的长度 |
getAbsolutePath() | String | 获取文件的绝对路径 |
getParent() | String | 获取文件的父路径 |
isFile() | boolean | 判断文件是否存在 |
isDirectory() | boolean | 判断文件是否为一个目录 |
isHidden() | boolean | 判断文件是否为隐藏文件 |
lastModified() | long | 获取文件最后修改时间 |
//读取文件中的内容,最好的方式是设置缓冲区。
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Lian03zuoye {
public static void main(String[] args) throws IOException {
try {
Reader fr =new FileReader("C:\\myFile\\my Prime.txt");
char[] c= new char[3]; //设置缓冲区
StringBuilder sBuilder=new StringBuilder();
int i =0;
while((i=fr.read(c))!=-1) {
System.out.println(123);//每3个字符(缓冲区的长度)循环一次
String we =new String(c,0,i);
//一定要加i来限定字符串长度,则分割后的字符串最后一个字符串中的空字符就不会被自动补充。
sBuilder.append(we);
}
System.out.println(sBuilder);
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
//文件读取和文件复制
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Lian02zuoye12 {
@SuppressWarnings("null")
public static void main(String[] args) throws IOException {
//练习1
// File file = new File("D:\\wen.java");
// file.createNewFile();
// System.out.println("文件是否存在:"+file.exists());
// System.out.println("文件是否是文件:"+file.isFile());
// System.out.println("文件是否是目录:"+file.isDirectory());
// System.out.println("文件相对路径名:"+file.getPath());
// System.out.println("文件绝对路径名:"+file.getAbsolutePath());
// System.out.println("文件名字:"+file.getName());
// System.out.println("文件大小:"+(file.length()/1000+file.length()%1000*0.001)+"KB");
// file.delete();
//方法不好舍去
// FileInputStream fis =new FileInputStream("D:\\JAVA\\课堂资料\\java高级\\day02\\CollectionsTest02\\src\\suffle\\Student.java");
// System.out.println(fis.available());
// try {
// for (int i = 0; i < fis.available()*10000; i++) {
// System.out.print((char)fis.read());
//
// }
// } catch (Exception e) {
// // TODO: handle exception
// }finally {
// fis.close();
// }
// FileOutputStream fos = new FileOutputStream("D:\\\\JAVA\\\\课堂资料\\\\java高级\\\\day02\\\\CollectionsTest02\\\\src\\\\suffle\\\\Student.java");
// String a = "好好学习,天天向上";
// byte[] words = a.getBytes();
// //fos.write(102);//输入一个int类型的编码
// fos.write(words, 0, words.length);
//练习2
//写入文件内容
File file = new File("D:\\我的青春谁做主.txt");
file.createNewFile();
String k = "我的青春当然是我做主啦!!";
byte[] word = k.getBytes();
//String kk = new String(word,"utf-16");
FileOutputStream o =new FileOutputStream(file);
o.write(word, 0, word.length);
o.flush();
o.close();
//拿出文件里的内容
FileInputStream a =new FileInputStream("D:\\我的青春谁做主.txt");
byte tt[] = new byte[1024];
int len = 0;
int rrad =0;
while ((rrad=a.read())!=-1) {
tt[len]=(byte)rrad;
len++;
}
String we = new String(tt,0,len);
System.out.println(we);
o.flush();
a.close();
//再将拿出的内容放入要复制的文件中
String s=we.toString();
File outFile = new File("C:\\myFile");
outFile.mkdirs();
FileOutputStream d = new FileOutputStream("C:\\myFile\\my Prime.txt");
byte[] words = s.getBytes();
d.write(words, 0, words.length);
o.flush();
d.close();
//输入两个文件File source, File dest
// InputStream input = null;
// OutputStream output = null;
// try {
// input = new FileInputStream(source);
// output = new FileOutputStream(dest);
// byte[] buf = new byte[1024];
// int bytesRead;
// while ((bytesRead = input.read(buf)) > 0) {
// output.write(buf, 0, bytesRead);
// }
// } finally {
// input.close();
// output.close();
// }
}
//Files类 复制两个文件(java7)
public void name(File a,File b) throws IOException {
Files.copy(a.toPath(), b.toPath());
}
//使用Commons IO复制
// private static void copyFileUsingApacheCommonsIO(File source, File dest)
// throws IOException {
// FileUtils.copyFile(source, dest);
// }
//使用FileChannel复制
private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
}
//使用FileStreams复制
private static void copyFileUsingFileStreams(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
}
}