JAVA笔记11__File类/File类作业/字节输出流、输入流/字符输出流、输入流/文件复制/转换流...
生活随笔
收集整理的這篇文章主要介紹了
JAVA笔记11__File类/File类作业/字节输出流、输入流/字符输出流、输入流/文件复制/转换流...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** File類:文件的創建、刪除、重命名、得到路徑、創建時間等,是唯一與文件本身有關的操作類*/
public class Main {public static void main(String[] args) {//File.separator 表示分隔符File f1 = new File("c:" + File.separator + "fuck" + File.separator + "javaTest1.txt");String s1 = File.pathSeparator; //路徑分隔符System.out.println(File.separator + " " + s1);boolean b1 = f1.exists(); //文件是否存在
System.out.println(b1);if (!b1) {try {boolean bt1 = f1.createNewFile(); //創建文件
System.out.println(bt1);} catch (IOException e) {e.printStackTrace();}}System.out.println(f1.delete()); //刪除文件//System.out.println(""); 快捷鍵:打sout,然后中按Tab鍵 System.out.println(f1.getParent()); //得到文件的上一級路徑
System.out.println(f1.isDirectory()); //判斷是否是目錄
File f2 = new File("c:" + File.separator + "fuck" + File.separator);String[] fname = f2.list(); //列出文件夾中的所有文件名for(String i:fname) System.out.println(i);File[] files = f2.listFiles(); //列出文件中的所有文件,以file數組返回for(File i:files) System.out.println(i.getName()+" "+i.length());File f3 = new File("c:\\fuck\\JavaTest1"); System.out.println(f3.mkdir()); //創建文件夾
f3.delete();System.out.println(f3.renameTo(new File("c:\\fuck\\JavaTest2"))); //重命名
}
}
?
/*** 在某個目錄中找到某個擴展名的所有文件*/ public class Main {private static int num = 0;public static void findFile(File f,String extName){if(f==null)return;else{if(f.isDirectory()){File[] fs = f.listFiles();if(fs!=null){for(File i:fs)findFile(i,extName);}}else{String path = f.getPath().toLowerCase();if(path.endsWith(extName)){System.out.println(f.getPath());++num;}}}}public static void main(String[] args) {File f = new File("c:\\fuck\\");String extName = ".cpp";findFile(f,extName);System.out.println(num);} }?
/*** IO流:輸入輸出流* 流:一組有順序的,有起點和終點的字節集合,是對數據傳輸的總稱或抽象。* 流的本質是數據傳輸,根據數據傳輸特性將流抽象為各種類,方便更直觀的進行數據操作。* 根據處理數據類型的不同分為:1.字符流 2.字節流* 根據數據流向不同分為:1.輸入流(程序從外部讀取) 2.輸出流(程序將數據寫到外部)* * OutputStream類:接受輸出字節并將這些字節發送到某個接收器*/ public class Main {public static void main(String[] args) {//write1(); write2();System.out.println("finished.");}/*** 字節輸出流方式一:每次輸出一個字節*/public static void write1(){OutputStream out = null;try {out = new FileOutputStream("c:\\fuck\\javaTest1.txt");String info = "helloIO";byte[] bs = info.getBytes();for(int i=0;i<bs.length;++i)out.write(bs[i]);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally {try {out.close();} catch (IOException ex) {ex.printStackTrace();}}}/*** 字節輸出流方式二:每次輸出指定大小字節*/public static void write2(){OutputStream out = null;try {out = new FileOutputStream("c:\\fuck\\javaTest1.txt",true); //參數true表示追加輸出 String info = "hello fish7";byte[] bs = info.getBytes();//out.write(bs);out.write(bs,0,5);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally {try {out.close();} catch (IOException ex) {ex.printStackTrace();}}} }?
public class Main {public static void main(String[] args) {//read1(); // read2(); read3();System.out.println("finished.");}/*** 字節輸入流的讀取方式一:每次讀取一個字節*/public static void read1(){InputStream in = null;try {in = new FileInputStream("c:\\fuck\\javaTest1.txt");int bs = -1; //定義一個字節,-1表示沒有數據while((bs = in.read())!=-1){System.out.print((char)bs); }System.out.println("");} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally{try {in.close();} catch (IOException ex) {ex.printStackTrace();}}}/*** 字節輸入流的讀取方式二:一次性讀取所有字節(適合不太大的文件)*/public static void read2(){InputStream in = null;try {File f = new File("c:\\fuck\\javaTest1.txt");in = new FileInputStream(f);byte[] bs = new byte[(int)f.length()]; //根據文件大小構造字節數組int len = in.read(bs);System.out.println(new String(bs));System.out.println("len = "+len);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally{try {in.close();} catch (IOException ex) {ex.printStackTrace();}}}/*** 字節輸入流的讀取方式三:每次讀取指定大小的字節(折中的方法)*/public static void read3(){InputStream in = null;try {File f = new File("c:\\fuck\\javaTest1.txt");in = new FileInputStream(f);byte[] bs = new byte[5];int len = -1; //每次讀取的實際長度StringBuilder sb = new StringBuilder();while((len=in.read(bs))!=-1){sb.append(new String(bs,0,len));}System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally{try {in.close();} catch (IOException ex) {ex.printStackTrace();}}} }?
/*** 字符流:底層也是用字節流實現的*/ public class Main {public static void main(String[] args) {writer1();System.out.println("finished.");}/*** 字符輸出流方式一:以字符數組方式輸出*/public static void writer1(){Writer out = null;try {File f = new File("c:\\fuck\\javaTest1.txt");out = new FileWriter(f,true); //true表示追加方式 String info = "good good study, day day up.";//out.write(info.toCharArray()); out.write(info);} catch (IOException ex) {ex.printStackTrace();} finally{try{out.close();} catch (IOException ex) {ex.printStackTrace();}}} }?
/*** 字符流:底層也是用字節流實現的*/ public class Main {public static void main(String[] args) {//byteReader(); //輸出一堆亂碼 reader1();System.out.println("finished.");}/*** 字符輸入流方式一:使用指定大小的字符數組輸入*/public static void reader1(){File f = new File("c:\\fuck\\javaTest1.txt");try {Reader in = new FileReader(f);char[] cs = new char[10];int len = -1;StringBuilder sb = new StringBuilder();while((len=in.read(cs))!=-1){sb.append(new String(cs,0,len));}in.close();System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}/*** 使用字節流讀取文本文件*/public static void byteReader(){File f = new File("c:\\fuck\\javaTest1.txt");try {InputStream in = new FileInputStream(f);byte[] bs = new byte[10];int len = -1;StringBuilder sb = new StringBuilder();while((len=in.read(bs))!=-1){sb.append(new String(bs,0,len));}in.close();System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}} }?
/*** 字符流:底層也是用字節流實現的* :根據指定的編碼,將1個或多個字節轉化為java里的unicode字符,然后進行操作* 字符操作一般用Writer,Reader等,字節操作一般用InputStream,OutputStream以及各種包裝類,比如:* BufferedInputStream和BufferedOutputStream等* 總結:如果你確認你要處理的流是可打印的字符,那么使用字符流會簡單些,如果不確認,那么用字節流總是不會錯的。*/ public class Main {public static void main(String[] args) {//byteReader(); //輸出一堆亂碼 reader1();System.out.println("finished.");}/*** 字符輸入流方式一:使用指定大小的字符數組輸入*/public static void reader1(){File f = new File("c:\\fuck\\javaTest1.txt");try {Reader in = new FileReader(f);char[] cs = new char[10];int len = -1;StringBuilder sb = new StringBuilder();while((len=in.read(cs))!=-1){sb.append(new String(cs,0,len));}in.close();System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}/*** 使用字節流讀取文本文件*/public static void byteReader(){File f = new File("c:\\fuck\\javaTest1.txt");try {InputStream in = new FileInputStream(f);byte[] bs = new byte[10];int len = -1;StringBuilder sb = new StringBuilder();while((len=in.read(bs))!=-1){sb.append(new String(bs,0,len));}in.close();System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}} }?
/*** 指定一個盤符下的文件,把該文件復制到指定的目錄下。*/ public class Main {public static void main(String[] args) {copyFile("c:\\fuck\\javaTest1.txt","c:\\fuck\\javaTest2.txt");copyFile("c:\\fuck\\ning.bmp","c:\\fuck\\ning2.txt"); //把.txt改成.bmp就可以看了System.out.println("finished.");}public static void copyFile(String src,String des){File f1 = new File(src);File f2 = new File(des);InputStream in = null;OutputStream out = null;try {in = new FileInputStream(f1);out = new FileOutputStream(f2);byte[] bs = new byte[105];int len = -1;while((len=in.read(bs))!=-1){out.write(bs,0,len);}} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();} finally{try {in.close();out.close();} catch (IOException ex) {ex.printStackTrace();}}} }?
/*** 轉換流:將一個字節流轉換為字符流,也可以將一個字符流轉換為字節流* OutputStreamWriter:可以將輸出的字符流轉換為字節流的輸出形式,可使用指定的charset將要寫入流* 中的字符編碼成字節。* InputStreamReader:將輸入的字節流轉換為字符流輸入形式,使用指定的charset讀取字節并將其解碼為* 字符。*/ public class Main {public static void main(String[] args) {//writer(); reader();System.out.println("finished.");}public static void writer(){ //把字符流轉成字節流try {OutputStream out = new FileOutputStream("c:\\fuck\\javaTest1.txt"); String info = "你好嗎abc.";Writer w1 = new OutputStreamWriter(out); //通過字節輸出流構造一個字符輸出流 w1.write(info);w1.close();out.close();} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}public static void reader(){ //把字節流轉成字符流try {InputStream in = new FileInputStream("c:\\fuck\\javaTest1.txt"); Reader r1 = new InputStreamReader(in);char[] cs = new char[105];int len = -1;StringBuilder sb = new StringBuilder();while((len=r1.read(cs))!=-1){sb.append(new String(cs,0,len));}r1.close();in.close();System.out.println(sb);} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}} }?
轉載于:https://www.cnblogs.com/fish7/p/4149726.html
總結
以上是生活随笔為你收集整理的JAVA笔记11__File类/File类作业/字节输出流、输入流/字符输出流、输入流/文件复制/转换流...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用反射写的取属性值和设置属性值得方法
- 下一篇: IOS屏幕布局