1、读取文件 public static void test1(){ //读入文件地址 File file=new File("d:/aaaa.txt"); //文件是否存在 System.out.println(file.exists()); //文件的字节长度 System.out.println(file.length()); //最后修改日期 System.out.println(file.lastModified()); //更改最后修改日期 Date date =new Date(file.lastModified()); //打印日期 System.out.println(date); //设置日期格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //打印日期 System.out.println(sdf.format(date)); }
2、打印文件目录
public static void test2() { //读入指定文件夹 File dirs = new File("d:/abc"); //创建文件集合 File[] files = dirs.listFiles(); //遍历 for (File f : files) { //判断f是否是目录 if (f.isDirectory()) { System.out.println("--" + f.getName()); //访问目录中的内容 File[] file1s = f.listFiles(); for (File f2 : file1s) { System.out.println(f2.getName()); } System.out.println("--"); } else { System.out.println(f.getName()); } }
3、显示文件夹下的文件列表(下一级文件前加空格)
方法一:获取指定空格数的空格字符串 public static String getSpaceString(int count){ StringBuffer sb=new StringBuffer(""); for (int i=0;i<count;i++){ sb.append(" "); } return sb.toString(); }
方法二:显示文件夹下的文件列表(参数 父目录,空格数量 )
public static void showFileList(File dir,int spaceCount){ File[] files = dir.listFiles(); //获得空格字符串 String spaceString = getSpaceString(spaceCount); //遍历 for (File file :files) { System.out.println(spaceString+ file.getName()); if (file.isDirectory()){ //递归、遍历file目录的文件列表 showFileList(file,spaceCount+1); } } }
4、使用read()读文件
//不能读汉字
public static void fileInputeStream1(){ //创建输入流 try { FileInputStream fi=new FileInputStream("d:/abc/w.txt"); //读到文件末尾返回-1; do { int re = fi.read(); if (re==-1){ break; } //转成字符输出 System.out.print((char) re); }while (true); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
5、字节数组读文件
public static void fileInputeStream2(){ try { FileInputStream fi=new FileInputStream("d:/abc/w.txt"); //使用字节数组读取 byte[] bs=new byte[20]; //20个字节为一行 //读到结尾返回-1 int re=-1; //re=fi.read(bs)等于字节长度 while ((re=fi.read(bs))!=-1){ // 0 开始,re结束 System.out.print(new String(bs,0,re)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
6、write(byte)写文件
public static void fileOutputeStream1(){ try { //会覆盖原文件: FileOutputStream fo = new FileOutputStream("d:/abc/w.txt"); //在原文件后增写。 //FileOutputStream fo = new FileOutputStream("d:/abc/w.txt",true); String words="hello world!!"; byte[] bytes =words.getBytes(); fo.write(bytes); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
7、复制文件
public static void fileCopy1(){ FileInputStream fi=null; FileOutputStream fo=null; try { //读文档 fi=new FileInputStream("d:/abc/w.txt"); //写文档 fo = new FileOutputStream("d:/abc/w1.txt"); byte[] bs =new byte[10]; int re=-1; while ((re=fi.read(bs))!=-1){ //把读到的字节数组写入文件 fo.write(bs,0,re); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (fo!=null){ try { if (fo!=null){ fo.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
8、字符流读文件
public static void test8(){ FileReader fr=null; try { fr =new FileReader("d:/abc/w.txt");z char[] cs = new char[11]; int length=-1; while ((length=fr.read(cs))!=-1){ System.out.println(new String(cs,0,length)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fr!=null){ try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } }
9、字符流读文件(转文件编码格式)
public static void test9(){ // FileReader fr=null; FileInputStream fi=null; InputStreamReader ir=null; try { // fr =new FileReader("d:/abc/w.txt"); fi=new FileInputStream("d:/abc/w.txt"); //字节流转字符流 ir =new InputStreamReader(fi,"UTF-8"); char[] cs = new char[11]; int length=-1; while ((length=ir.read(cs))!=-1){ System.out.println(new String(cs,0,length)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (ir!=null){ try { ir.close(); } catch (IOException e) { e.printStackTrace(); } } } }
10、BufferedReader读文件
public static void test4(){ BufferedReader br =null; try { FileReader fr=new FileReader("d:/abc/w.txt"); br =new BufferedReader(fr); //一次读一行 String content =null; while ( (content =br.readLine())!=null){ System.out.println(content); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } }
11、BufferedWriter
//带缓冲区,大量数据不用一次性直接写入磁盘,提高效率,
//当缓冲区满了以后,会自动写入磁盘,或者调用flush方法清空缓冲区时写入磁盘
//调用close方法会自动调用flush
public static void test11(){ BufferedWriter bw =null; try { FileWriter fw=new FileWriter ("d:/abc/w2.txt"); bw =new BufferedWriter(fw); bw.write("你好,hello"); } catch (IOException e) { e.printStackTrace(); }finally { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } }
12、替换文件中文字
public static void test8(){ BufferedWriter bw =null; BufferedReader br =null; try { FileReader fr =new FileReader("d:/abc/pet.template"); FileWriter fw =new FileWriter("d:/abc/pet.txt"); //高级流 br=new BufferedReader(fr); bw=new BufferedWriter(fw); //读取模板 String content =null; while ((content= br.readLine())!=null){ //替换内容 content= content.replace("{name}","QQ"); content= content.replace("{type}","呆企鹅"); content= content.replace("{master}","爸爸"); System.out.println(content); bw.write(content); bw.newLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { br.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } }