流是一组有序的数据序列,分为输入流和输出流。IO操作主要是对文件进行操作,而所有的IO操作都在java.io包中,主要是:File,InputStream,OutputStream,Reader,Writer和Serializable。 File操作
public static void main(String[] args)throws Exception{ File file=new File("d:"+File.separator+"demo"+File.separator+"text8.txt"); //判断父目录是否存在,如果不存在,创建目录,创建文件 if (!file.getParentFile().exists()){ file.getParentFile().mkdirs(); file.createNewFile(); }else { //如果父目录存在,判断文件是否存在,不存在创建新文件 if (!file.exists()) { file.createNewFile(); System.out.println("创建新文件!"); }} System.out.println("文件存在!"); }程序结果:文件存在
public static void main(String[] args)throws Exception{ File file=new File("d:"+File.separator+"demo"+File.separator+"text8.txt"); if (!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } if (file.exists()) { System.out.println("文件名称:"+file.getName()); System.out.println("文件长度:"+file.length()); System.out.println("是文件:"+file.isFile()); }程序结果: 文件名称:text8.txt 文件长度:0 是文件:true
目录查询
public static void main(String[] args)throws Exception { File file=new File("d:"+File.separator+"demo"); if (file.isDirectory()){ File result[]=file.listFiles(); for (int x=0;x<result.length;x++){ System.out.println(result[x]); } } }程序结果: d:\demo\testA.txt d:\demo\testB.txt d:\demo\text8.txt
字节流与字符流 outputStream针对的内存而言,是向文件写入。
public static void main(String[] args)throws Exception{ File file=new File("d:"+File.separator+"demo"+File.separator+"text8.txt"); if (!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } OutputStream os=new FileOutputStream(file); String str="阿珍爱上了阿强"; byte data[]=str.getBytes(); os.write(data); os.close(); System.out.println("写入成功"); }程序结果:写入成功 InputStream针对文件读取
public static void main(String[] args)throws Exception{ File file=new File("d:"+File.separator+"demo"+File.separator+"text8.txt"); if (file.exists()){ InputStream is=new FileInputStream(file); byte data[]=new byte[1024]; int foot=0; int temp=0; while ((temp=is.read(data))!=-1){ data[foot++]=(byte) temp; } is.close(); System.out.println(new String(data,0,foot)); } }程序结果:阿珍爱上了阿强 使用系统自带的记事本打开会出现乱码。
创建文件输入“阿珍爱上了阿强”并且读取。
public static void main(String[] args) throws IOException { File file=new File("d:"+File.separator+"demo"+File.separator+"test.txt"); if (file.exists()) { file.delete(); System.out.println("已有文件删除!"); }else { try { file.createNewFile(); System.out.println("创建文件!"); } catch (Exception e) { e.printStackTrace(); } } if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } try { OutputStream os=new FileOutputStream(file); String str="阿珍爱上了阿强"; byte data[]=str.getBytes(); os.write(data); os.close(); } catch (Exception e) { e.printStackTrace(); } if (file.exists()) { InputStream is=new FileInputStream(file); byte data[]=new byte[1024]; int temp=0; int foot=0; while ((temp=is.read())!=-1) { data[foot++]=(byte)temp; } is.close(); System.out.println(new String(data,0,foot)); } }文件复制,需要先初始化文件
public static void main(String[] args)throws Exception{ long start=System.currentTimeMillis(); if (args.length!=2) { System.out.println("命令错误"); System.exit(1); } File inFile=new File(args[0]); //源文件路径 if (!inFile.exists()){ System.out.println("文件不存在"); System.exit(1); } File outFile=new File(args[1]); //复制文件路径 if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } InputStream is=new FileInputStream(inFile); OutputStream os=new FileOutputStream(outFile); byte data[]=new byte[1024]; int temp=0; while ((temp=is.read(data))!=-1){ os.write(data,0,temp); } is.close(); os.close(); long end=System.currentTimeMillis(); System.out.println("文件复制时间:"+(end-start)); }缓存是对I/O的性能优化,,缓存流增加了内存缓存区。字符缓冲区流:BufferedReader、BufferedWriter。字节缓冲区流:BufferedInputStream、BufferedOutputStream。
public static void main(String[] args) { File file=new File("d:"+File.separator+"demo"+File.separator+"test.txt"); String content[]= {"阿珍","爱上了","阿强"}; if (file.exists()) { try { FileWriter fw=new FileWriter(file); BufferedWriter bw=new BufferedWriter(fw); for (int i = 0; i < content.length; i++) { bw.write(content[i]); //遍历输出 bw.newLine(); //每一个元素换行显示 } bw.close(); fw.close(); } catch (Exception e) { e.printStackTrace(); } } if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } try { FileReader fr=new FileReader(file); BufferedReader br=new BufferedReader(fr); String str=null; while ((str=br.readLine())!=null) { System.out.println(str); } br.close(); fr.close(); } catch (Exception e) { e.printStackTrace(); } }程序结果: 阿珍 爱上了 阿强