1.完成文件和目录操作(File) public static void main(String[] args) { // 完成文件和目录操作(File) File f1=new File("D:\\t.txt"); File f2=new File("k.txt"); File f3=new File("D:\\abt"); File f4=new File("D:\\abt\\egd"); try { boolean b1=f1.createNewFile(); boolean b2=f2.createNewFile(); boolean b3=f3.mkdir(); boolean b4=f4.mkdirs(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
2.字节流的读取和写入 public static void main(String[] args) { // 字节流的读取和写入 FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("d:\\ye.jpg"); fos = new FileOutputStream("d:\\yyy.jpg"); byte[] b = new byte[1024]; int legth = 0; while ((legth = fis.read(b)) != -1) { fos.write(b, 0, legth); } } catch (IOException e) { e.printStackTrace(); } finally { } }
3. 字符流的读取和写入 public static void main(String[] args) { // 字符流的读取和写入 BufferedReader br=null; BufferedWriter bw=null; try { br = new BufferedReader(new FileReader("d:\\abs.txt")); bw=new BufferedWriter(new FileWriter("d:\\abC.txt")); String t=""; while ((t=br.readLine())!=null){ bw.write(t); bw.newLine(); } bw.flush(); } catch (java.io.IOException e) { e.printStackTrace(); } }
4.字节流向字符流的转换 public static void main(String[] args) { // 字节流向字符流的转换 BufferedWriter bw=null; BufferedReader br =null; br = new BufferedReader(new InputStreamReader(System.in)); try { bw = new BufferedWriter(new FileWriter("d:\\abs.txt")); String t=""; while ((t=br.readLine())!=null){ if (t.equals("kkk")){ break; } bw.write(t); bw.newLine(); } bw.flush(); } catch (IOException e) { e.printStackTrace(); } }