构造器 | 描述 |
---|---|
File(String pathname) | 由路径名称来建立 |
File(String parent, String child) | 由上层目录路径+文件名来建立 |
File(File parent, String child) | 由上层文件名+文件名来建立 |
File(URI uri) | 由uri来建立 |
路径java
路径分隔符面试
public static final String separator
,根据操做系统,动态的提供分隔符。File file1 = new File("d:\\atguigu\\info.txt"); File file2 = new File("d:" + File.separator + "atguigu" + File.separator + "info.txt"); File file3 = new File("d:/atguigu");
方法 | 描述 |
---|---|
getAbsolutePath() | 获取绝对路径 |
getPath() | 获取路径 |
getName() | 获取名 |
getParent() | 获取上层文件目录路径。若无,返回null |
length() | 获取文件长度(即:字节数)。不能获取目录的长度。 |
lastModified() | 获取最后一次的修改时间,毫秒值 |
下面两个是针对目录的 | |
public String[] list() | 获取指定目录下的全部文件或者文件目录的名称数组 |
public File[] listFiles() | 获取指定目录下的全部文件或者文件目录的File数组 |
public boolean renameTo(File dest)
:把文件重命名为指定的文件路径。(实际上就是把file1的内容复制到file2,并把file1删除)windows
对于 file1.renameTo(file2)
要求:file1存在,file2不存在数组
方法 | 描述 |
---|---|
isDirectory() | 判断是不是文件目录 |
isFile() | 判断是不是文件 |
exists() | 判断是否存在 |
canRead() | 判断是否可读 |
canWrite() | 判断是否可写 |
isHidden() | 判断是否隐藏 |
方法 | 描述 | 注意事项 |
---|---|---|
public boolean createNewFile() | 建立文件 | 若文件存在,则不建立,返回false |
public boolean mkdir() | 建立文件目录 | 若是此文件目录存在,就不建立; 若是此文件目录的上层目录不存在,也不建立 |
public boolean mkdirs() | 建立文件目录 | 若是上层文件目录不存在,一并建立 |
public boolean delete() | 删除文件或者文件夹 | Java中的删除不走回收站 要删除的文件目录内不能包含文件或文件目录 |
若是你建立文件或者 文件 目录没有写盘符路径 , 那么 ,默认在项目路径下。网络
√√√按操做的数据单位不一样分为:字节流(8bit)、字符流(16bit)。字节流适合操做图片、视频等文件,字符流适合操做文本文件。多线程
按数据流的流向不一样分为:输入流、输出流。app
按流的角色不一样分为:**节点流、处理流。dom
节点流:直接从数据源或目的地读写数据。也叫文件流ide
处理流:不直接链接到数据源或目的地,而是“链接”在已存 在的流(节点流或处理流)之上,经过对数据的处理为程序提 供更为强大的读写功能单元测试
抽象基类 | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
public void test1() throws IOException { //1.实例化File类,指明要操做的对象.这一步的目的是创建硬盘中的文件和Java中类的对应关系. File file = new File("hello1.txt"); //2.提供具体的流.参数的做用就是帮助咱们并链接上文件这个"大水库" FileReader fileReader = new FileReader(file); //3.用流读取到内存 //read():返回读入的字符,是int须要转换为char.到了文件结尾返回-1 int read = fileReader.read(); while (read != -1) { System.out.print((char) read); read = fileReader.read(); } //4.关闭流 fileReader.close(); } //整个过程结合图示去理解很合理
/* 优化: 1.第三部能够在语法上的优化,可是效率实际上是同样的 2.为了保证关闭操做必定执行,使用try-catch-finally 3.读入的文件必定要存在,不然会出现:FileNotFoundException */ public void test2() { FileReader fileReader = null; try { File file = new File("hello1.txt"); fileReader = new FileReader(file); //改进1 int read; while ((read = fileReader.read()) != -1){ System.out.print((char) read); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileReader != null) fileReader.close(); } catch (IOException e) { e.printStackTrace(); } }
//使用数组 char[] charBuffer = new char[5]; int len;//记录每次读入到charBuffer数组中的字符个数 while ((len = fileReader.read(charBuffer)) != -1){ for (int i = 0; i < len; i++) {//这里要用len(读取的字符数)二不是数组的长度 System.out.print(charBuffer[i]); } } //固然for循环也能够换位String的构造器来把字符串数组转换为String String string = new String(charBuffer, 0, len); System.out.print(string);
File file = new File("hello2.txt"); FileWriter fw = new FileWriter(file); fw.write("i have a dream!"); fw.close(); //最后用try-catch处理一下异常,上面的步骤更清晰一些
public void test5() throws IOException { File srcFile = new File("hello2.txt"); File destFile = new File("hello3.txt"); FileReader fr = new FileReader(srcFile); FileWriter fw = new FileWriter(destFile); char[] charBuffer = new char[5]; int len; while ((len = fr.read(charBuffer)) != -1) { fw.write(charBuffer, 0, len);//和用String来取是相似的★★★★★ } fw.close(); fr.close(); } //最后用try-catch处理一下异常,上面的步骤更清晰一些
BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter
public void test() throws IOException { //1.建立File File srcFile = new File("img1.png"); File destFile = new File("img2.png"); //2.建立流 //2.1建立文件流 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); //2.2建立字节流 BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); //3.复制 byte[] bytes = new byte[10]; int len; while ((len = bis.read(bytes)) != -1){ bos.write(bytes,0,len); } //4.关闭流 bis.close(); bos.close(); }
public void test1() throws IOException { //1.建立文件和流 BufferedReader br = new BufferedReader(new FileReader(new File("hello1.txt"))); BufferedWriter bw = new BufferedWriter(new FileWriter(new File("hello4.txt"))); // //2.复制 // char[] chars = new char[10]; // int len; // while ((len = br.read(chars)) != -1) { // bw.write(chars, 0, len); // } // 复制:用String来实现★★★★★★★★★★★ String data;//可是是不带换行的,能够用一如下两种方法实现 while ((data = br.readLine()) != null) { // //方法一★★★★★★★★ // bw.write(data + "\n"); //方法二★★★★★★★★ bw.write(data); bw.newLine(); } //3.关闭 br.close(); bw.close(); }
public InputStreamReader(InputStream in)
默认使用utf-8字符集public InputSreamReader(InputStream in,String charsetName)
能够本身选择字符集。按照地区辅助记忆 | 编码表 | 描述 |
---|---|---|
美国 | ASCII | 用一个字节的7位来表示全部英文和符号 |
欧洲 | ISO8859-1 | 用一个字节的8位表示全部欧洲语言的字母 |
中国 | GB2312 GBK |
最多两个字节编码全部汉字 升级版,加入了更多的汉字 |
国际通用 | Unicode UTF-8 |
Unicode编码是对UTF-8/16的统称 用1-4个字节表示人类全部文字 |
@Test public void test2() throws Exception { //1.造文件、造流 File file1 = new File("dbcp.txt"); File file2 = new File("dbcp_gbk.txt"); FileInputStream fis = new FileInputStream(file1); FileOutputStream fos = new FileOutputStream(file2); InputStreamReader isr = new InputStreamReader(fis,"utf-8"); OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk"); //2.读写过程 char[] cbuf = new char[20]; int len; while((len = isr.read(cbuf)) != -1){ osw.write(cbuf,0,len); } //3.关闭资源 isr.close(); osw.close(); }
public class Exercise { public static void main(String[] args) {//idea不支持在单元测试中输入内容,因此改用main()来测试 BufferedReader br = null; try { InputStreamReader isr = new InputStreamReader(System.in); br = new BufferedReader(isr); while (true) { System.out.println("请输入字符串: "); String data = br.readLine(); if ("e".equalsIgnoreCase(data)||"exit".equalsIgnoreCase(data)){ System.out.println("程序结束"); break; } String upperCase = data.toUpperCase(); System.out.println(upperCase); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
PrintStream ps = null; try { FileOutputStream fos = new FileOutputStream(new File("D:\\IO\\text.txt")); // 建立打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区) ps = new PrintStream(fos, true); if (ps != null) {// 把标准输出流(控制台输出)改为文件 System.setOut(ps); } for (int i = 0; i <= 255; i++) { // 输出ASCII字符 System.out.print((char) i); if (i % 50 == 0) { // 每50个数据一行 System.out.println(); // 换行 } } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (ps != null) { ps.close(); } }
引入:为了方便地操做Java语言的基本数据类型和String的数据,可使用数据流
数据流有两个类:(用于读取和写出基本数据类型、String类的数据,方便持久化)
DataInputStream 和 DataOutputStream
DataInputStream中的方法
boolean readBoolean()
char readChar()
double readDouble()
long readLong()
String readUTF()
byte readByte()
float readFloat()
short readShort()
int readInt() void
readFully(byte[] b)
DataOutputStream中的方法
将上述的方法的read改成相应的write便可
练习:将内存中的字符串、基本数据类型的变量写出到文件中。 注意:处理异常的话,仍然应该使用try-catch-finally. */ @Test public void test3() throws IOException { //1. DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt")); //2. dos.writeUTF("刘建辰"); dos.flush();//刷新操做,将内存中的数据写入文件 dos.writeInt(23); dos.flush(); dos.writeBoolean(true); dos.flush(); //3. dos.close(); } /* 将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中。 注意点:读取不一样类型的数据的顺序要与当初写入文件时,保存的数据的顺序一致! */ @Test public void test4() throws IOException { //1. DataInputStream dis = new DataInputStream(new FileInputStream("data.txt")); //2. String name = dis.readUTF(); int age = dis.readInt(); boolean isMale = dis.readBoolean(); System.out.println("name = " + name); System.out.println("age = " + age); System.out.println("isMale = " + isMale); //3. dis.close(); }
什么是对象的序列化机制(面试题)
序列化的好处:可将任何实现了Serializable接 使其在保存和传输时可被还原。
public class ObjectInputOutputStream { /* 代码实现String类的对象的序列化和反序列化 */ @Test//序列化 public void testObjectOutputStream(){ ObjectOutputStream oos = null; try { //1.造流和文件 oos = new ObjectOutputStream(new FileOutputStream(new File("objectString.dat"))); //2.写出 oos.writeObject(new String("我爱你中国")); } catch (IOException e) { e.printStackTrace(); } finally { //3.关闭流 try { if (oos != null) { oos.close(); } } catch (IOException e) { e.printStackTrace(); } } } @Test public void testObjectInputStream(){ ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream(new File("objectString.dat"))); Object readObject = ois.readObject(); System.out.println(readObject); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (ois != null) { ois.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
public static final long serialVersionUID = xxxxxxxxL;
@Test public void test1() throws IOException { //用参数mode标识,让类表明输入 RandomAccessFile r = new RandomAccessFile(new File("hello1.txt"), "r"); //用参数mode标识,让类表明输出 RandomAccessFile rw = new RandomAccessFile(new File("hello5.txt"), "rw"); byte[] bytes = new byte[1024]; int len; while ((len=r.read(bytes)) != -1){ rw.write(bytes,0,len); } r.close(); rw.close(); }
/* 使用RandomAccessFile实现数据的插入效果 */ @Test public void test3() throws IOException { RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw"); raf1.seek(3);//将指针调到角标为3的位置 //保存指针3后面的全部数据到StringBuilder中 StringBuilder builder = new StringBuilder((int) new File("hello.txt").length()); byte[] buffer = new byte[20]; int len; while((len = raf1.read(buffer)) != -1){ builder.append(new String(buffer,0,len)) ; } //调回指针,写入“xyz” raf1.seek(3); raf1.write("xyz".getBytes()); //将StringBuilder中的数据写入到文件中 raf1.write(builder.toString().getBytes()); raf1.close(); }
public class JarTest { public static void main(String[] args) throws IOException { File srcFile = new File("s6_IO/hello1.txt"); File destFile = new File("s6_IO/hello6.txt"); FileUtils.copyFile(srcFile,destFile); } }