java.io.File类:表示了文件和目录
Windows和Unix中路径使用和路径分割 \和/ java中\表示转义,所以需要\\ 分隔符; :
java中使用常量来表示可以解决此问题 separator separatorChar 和 pathSeparator pathSeparatorChar : /
File构造方法
package file; import java.io.File; public class Demo1 { /** * @param args */ public static void main(String[] args) { File f1 = new File("/User/luffy/123.txt"); File f2 = new File("/User/luffy","123.txt"); File f3 = new File("/User/luffy"); new File(f3,"123.txt"); } }
常用方式
package _01_File; import java.io.File; import java.io.IOException; public class Demo1 { public static void main(String[] args) throws Exception { File file = new File("test"); System.out.println(file.exists()); System.out.println(file.isDirectory()); System.out.println(file.isFile()); new File("test2.txt").createNewFile(); new File("test/test2/test3").mkdirs(); System.out.println(file.getName()); //获取文件的相对路径 System.out.println(file.getPath()); //获取父目录 getParentFile()则返回的是File类型 System.out.println(file.getParent()); //获取绝对路径 System.out.println(file.getAbsolutePath()); //修改文件名称 file.renameTo(new File("xxoo")); new File("delete.txt").delete(); } }listFiles()
package _01_File; import java.io.File; public class ListFilesDemo { public static void main(String[] args) { File file = new File("D:/图"); listfiles(file); } static void listfiles(File file){ if(file.isFile()){ System.out.println(file); } if(file.isDirectory()){ File[] fs = file.listFiles(); if(null == fs){ return; } for (File file2 : fs) { //System.out.println(file2); listfiles(file2); } } } }
IO
表示输入/输出
java中的流分几种
1,从流向上考虑:输入/输出流
2,从流的单位上考虑:字节流,字符流
3,从流的功能上将:节点流/缓冲流
流的四大基类(抽象类)
文件输出流
package _02_IO; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class FileOutputStreamDemo { public static void main(String[] args) throws Exception { File file = new File("out.txt"); FileOutputStream fos = new FileOutputStream(file); String data = "hello io"; fos.write(data.getBytes()); fos.close(); } }
文件输入流
package _02_IO; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class FileInputStreamDemo { public static void main(String[] args) throws Exception { File file = new File("out.txt"); FileInputStream fis = new FileInputStream(file); //读取一个数据字节 //int f1 = fis.read(); //System.out.println(f1); //读取全部 byte[] buffer = new byte[1024]; int len = -1; while((len=fis.read(buffer)) != -1){ System.out.println(len); System.out.println(new String(buffer,0,len)); } fis.close(); } }使用字节流拷贝文件
package _02_IO; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class CopyFile { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { File in =new File("out.txt"); FileInputStream fis = new FileInputStream(in); FileOutputStream fos =new FileOutputStream("in.txt"); byte[] buffer = new byte[2]; int len=-1; while((len=fis.read(buffer)) != -1){ fos.write(buffer,0,len); } fos.close(); fis.close(); } }字符异常的处理
1.7以前
package _02_IO; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyFileTry_Catch { /** * @param args */ public static void main(String[] args) { FileInputStream fis = null; FileOutputStream fos = null; try{ fis = new FileInputStream("out.txt"); fos = new FileOutputStream("in.txt"); byte[] buffer = new byte[2]; int len=-1; while((len=fis.read(buffer)) != -1){ fos.write(buffer, 0, len); } }catch(Exception e){ e.printStackTrace(); }finally{ try { fos.close(); } catch (IOException e) { e.printStackTrace(); }finally{ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } }1.7后
package _02_IO; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CoptFile_Try_Catch_17 { /** * @param args */ public static void main(String[] args) { try( FileInputStream fis = new FileInputStream("out.txt"); FileOutputStream fos = new FileOutputStream("in.txt"); ){ int len = -1; byte[] buffer = new byte[2]; while((len=fis.read(buffer)) != -1){ fos.write(buffer,0,len); } }catch(IOException e){ e.printStackTrace(); } } }Scanner
package _03_scanner; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Demo1 { public static void main(String[] args) { //demo1(); Scanner sc = new Scanner(System.in); while(true){ String name = sc.next(); System.out.println("echo"+name); } } private static void demo1() throws FileNotFoundException { Scanner sc = new Scanner(new File("out.txt")); while(sc.hasNextLine()){ System.out.println(sc.nextLine()); } } }字符流
一般有中文使用,任何时候可以使用字节流,一般操作二进制文件,就只能使用字节流
FileWriter
FileReader
字节流的拷贝文件
package writerAndreader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.Reader; import java.io.Writer; public class Demo1 { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Reader reader = new FileReader("op.txt"); Writer writer = new FileWriter("mon.txt"); char[] buffer = new char[1024]; int len = -1; while((len=reader.read(buffer)) != -1){ writer.write(buffer, 0, len); } writer.close(); reader.close(); } }当只提供字节流时候可以通过InputStreamReader和OutputStreamWriter来进行转换
package writerAndreader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; public class Demo2 { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream("op.txt"); FileOutputStream fos = new FileOutputStream("qq.txt"); //如果这里只有字节输入和输出 FileInputStream OutputStream //那么通过什么来转换呢 Reader reader = new InputStreamReader(fis); Writer writer = new OutputStreamWriter(fos); //后面的就一样了 int len = -1; char[] buffer = new char[1024]; while((len=reader.read(buffer)) != -1){ writer.write(buffer, 0, len); } writer.close(); reader.close(); } }Files工具类
package writerAndreader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Demo3 { /** * @param args * @throws Exception */ @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { InputStream in = new FileInputStream("op.txt"); //这个是一种方法 //Files.copy(in,Paths.get("xxoo.txt")); //这个是第二种方法 URI uri = new File("tt.txt").toURI(); Path path = Paths.get(uri); Files.copy(in,path); } }