本文共 8524 字,大约阅读时间需要 28 分钟。
路径分隔符–File.pathSeparator
文件分隔符–File.seprarator路径表示形式
path = “E://xp//test/2.jpg”
path =”E:”+File.separator+”xp”+File.separator….课跨平台的路径 path = “E:\xp\test\2.jpg”–>path = “E:/xp/test/2.jpg”(推荐的方式)
常用的方法:
getName()
getParent()返回上一级目录,没有上一级返回空 exists()文件是否存在 canRead()文件是否可读 isFile() isDirectory() 判断 length()长度,字节数/文件夹无法读出长度 creatNewFile()创建文件(返回boolean类型)(若文件已经存在返回false) delete()删除文件返回(boolean) creatTempFile() 创建临时文件 deleteOnExit() 退出时自动删除 mkdir()创建目录,必须确保父目录存在 makirs()创建目录,递归创建目录。 String [] list()返回文件名称 File [] listFiles() 返回目录中的文件 listRoots() 根路径 FilenameFilter 过滤器
1 字节流:
输入流: InputStream ,read(byte[]),read(byte[],int off,int len),close()
FileInputStream() 输出流: OutputStream,write(byte[]),write(char [] ,int off,int len),flush(),close() FileOutputStream()
2 字符流:
输入流: Reader read(char []) ,read(char [] ,int off,int len),close()
FileReader() 输出流: Writer write(char []),writer(char [] ,int off,int len),flush(),close() FileWriter()
// 读取文件 public static void main(String [] args){ File f = new File("D:/桌面/a.txt"); byte [] file = new byte[1024]; InputStream in = null; try { in = new FileInputStream(f); int len = 0; while(-1 != (len = in.read(file))){ String s = new String(file, 0, len); System.out.print(s); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if (in!=null) in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
OutputStream 将数据写入到文件中,FileOutputStream将数据写入到流中
InputStream 将数据从文件中读取,FileInputStream将从六中读取数据 FileOutputStream(File , true)将数据追加到文件中,默认为false **一定一定一定要记得关闭流***
//写入到文件 public static void main(String [] args){ File file = new File("D:/桌面/b.txt"); OutputStream out = null; try { out = new FileOutputStream(file,false); String s = "写入文件操作\r\n"; byte [] s2 = s.getBytes(); out.write(s2); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if (out!=null){ try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
//复制文件 public static void main (String [] args){ File file1 = new File("D:/桌面/tree.jpg"); File file2 = new File("D:/桌面/trees.jpg"); InputStream input = null; OutputStream output = null; try { input = new FileInputStream(file1); output = new FileOutputStream(file2); byte [] data = new byte [1024*4]; int len = 0; while(-1 != (len = input.read(data))){ output.write(data,0, len); System.out.println(data); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("没有找到文件"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("写入成功"); } }
字符流:只能处理纯文本.txt .html
Writer FileWriter Reader FileReader 1 建立联系 2 选择流 Writer FileWriter 3 读取char[] dlush 4 关闭FileWriter(File ,true) 追加
append()追加
//FileReader 读取文件public class testFileReader { public static void main (String [] args){ File file = new File("D:/桌面/switch.txt"); File file2 = new File("D:/桌面/switch2.txt"); Reader input = null; Writer output = null; try { input = new FileReader(file); char [] data = new char [1024]; int len = 0; while (-1 != (len = input.read(data))){ String s = new String(data, 0, len); System.out.println(s); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("文件读取失败"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("关闭失败"); } } }}
//FileWriter复制文件public class testFileReader { public static void main (String [] args){ File file = new File("D:/桌面/switch.txt"); File file2 = new File("D:/桌面/switch2.txt"); Reader input = null; Writer output = null; try { input = new FileReader(file); output = new FileWriter(file2); char [] data = new char [1024]; int len = 0; while (-1 != (len = input.read(data))){ String s = new String(data, 0, len); output.write(s); // System.out.println(s); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("文件读取失败"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("关闭失败"); } try { output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("关闭输出流失败"); } } }}
1,字节缓冲流
BufferedInputStream
BufferedOutputStream
2,字符缓冲流
BufferedReader ReadLine()
BufferedWriter newLine()换行 字节流文件拷贝+缓冲流,提高性能 缓冲流(节点流) //使用新增方法不能发生多态
//用BufferedReader包装 BufferedReader input = null; BufferedWriter output = null; try { input = new BufferedReader(new FileReader(file)); output = new BufferedWriter(new FileWriter(file2)); String temp ; while(null != (temp = input.readLine())){ System.out.println(temp); }。。。。。。//输出方式同理,writer(String s )
转换流 : 字节流转字符流 处理乱码(编码集、解码集)
String(byte [] bytes,Charset charset) 使用charset编码来编码byte数组
String(byte [] bytes,int offset, int length,Charset charset) 编码byte数组的子数组 getbytes()使用默认的编码方式返回字节数组 getBytes(Charset charset)使用指定的编码方式返回字节数组
//处理文件编码 public static void main (String [] args) throws UnsupportedEncodingException{ String str1 = "字符串"; byte [] data = str1.getBytes("utf-8"); String str2 = new String(data, "utf-8"); System.out.println(str2); }
字节转为字符:(转换流)
InputStreamReader() OutputStreamWriter()//InputStreamReader(new FileInputStream(new File()))
//字节流转字符流 public static void main (String [] args) throws IOException{ File file1 = new File("D:/桌面/1.txt"); File file2 = new File("D:/桌面/2.txt"); try { BufferedReader input = new BufferedReader( new InputStreamReader( new FileInputStream(file1),"utf-8") ); String len ; while (null != (len = input.readLine())){ System.out.println(len); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }