java-IO操作實例

最近工作需要,開始學java!

關於java字節流和字符流的分類 只要記住:

1 :有編碼格式的是字符流

2:網絡傳輸的是字節流

2:加讀寫緩存不僅可以讀寫一行,還能夠減少讀取時間!

import java.io.*;

import com.sun.imageio.plugins.common.ReaderUtil;
import net.sf.json.JSON;
import net.sf.json.JSONObject;

public class My_io {
    public static void main(String[] args) throws IOException {
        // 三個測試方法
          //test01();
//        test02();
         // test03();
          test07();
    }

    public static void test01() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("請輸入一個字符");
        char c;
        c = (char) bufferedReader.read();
        System.out.println("你輸入的字符爲"+c);
    }

    public static void test02() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("請輸入一個字符,按 q 鍵結束");
        char c;
        do {
            c = (char) bufferedReader.read();
            System.out.println("你輸入的字符爲"+c);
        } while (c != 'q');
    }

    public static void test03() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("請輸入一行字符");
        String str = bufferedReader.readLine();
        System.out.println("你輸入的字符爲" + str);
    }
    // 二進制文件的讀寫
     public static  void test04() throws IOException {
        byte[] bytes = {1,2,3,4,5};
        FileOutputStream Out = new FileOutputStream(new File("").getAbsolutePath()+"test.txt");
        Out.write(bytes);
        Out.close();
        FileInputStream filein = new FileInputStream(new File("").getAbsolutePath()+"test.txt");
        int C;
        while((C = filein.read()) != -1) {
            System.out.print(C);
        }
     }
     //文本文件的讀寫
    public static void test05() throws IOException {
        FileWriter file_write = new FileWriter(new File("").getAbsolutePath() + "test1.txt");
        // 加 false,表示關閉追加
        FileWriter file_write1 = new FileWriter(new File("").getAbsolutePath() + "test1.txt",false);
        file_write.write("Hello,world! \n 歡迎來到java 的世界\n ");
        file_write.write("不會覆蓋文本文件的內容\n");
        file_write.append("並不是追殲一行的內容,不要被方法名迷惑\n");
        file_write.append(null);
        file_write.flush();
        System.out.println("文件的默認編碼爲" + file_write.getEncoding());
        file_write.close();

        FileReader file_read = new FileReader(new File("").getAbsolutePath() + "test1.txt");
        BufferedReader buffer_read = new BufferedReader(file_read);//這裏使用讀緩存,一次能夠讀一行
        String  str;
        while ((str = buffer_read.readLine()) !=null){
            System.out.println(str);
        }
        file_read.close();
        buffer_read.close();
        //這裏一次只讀取一個字符。
        FileReader File_read = new FileReader(new File("").getAbsolutePath() + "test1.txt");
        int c;
        while ((c = File_read.read()) != -1){
             System.out.print( (char) c );
            //System.out.print(  c );
        }
        File_read.close();
    }
    //使用字節流來處理,可指定文件的編碼
    public static void test06() throws IOException {
        FileOutputStream fileOut_stream = new FileOutputStream(new File("").getAbsolutePath() + "test2.txt");
        OutputStreamWriter out_writer = new OutputStreamWriter(fileOut_stream , "GBK");
        out_writer.write("hello! 歡迎來到 java的世界\n");
        out_writer.append("另外一行內容");
        out_writer.flush();
        System.out.println("文件編碼:" + out_writer.getEncoding());
        out_writer.close();
        fileOut_stream.close();
    }

    //讀json文件,返回字符串
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
           // 按照字符流,指定編碼格式讀取
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    //讀取json
    public static void test07() throws IOException {
        JSONObject girls = new JSONObject();
        //
        girls.put("name", "xiaoming");
        girls.put("age", 18);
        girls.put("hobboit", new String[]{ "讀書", "看電影"});
        System.out.println(girls.toString());
        BufferedWriter BW = new BufferedWriter(new FileWriter("newtest.json"));
        String ws = girls.toString();
        BW.write(ws);
        BW.flush();
        BW.close();

        //
        String path = My_io.class.getClassLoader().getResource("jsontest.json").getPath();
        String  input = readJsonFile(path);
        JSONObject  jobj = JSONObject.fromObject(input);
        System.out.println("request:" + jobj.get("request"));
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章