Java讀取檔案

來源:互聯網
上載者:User

標籤:輸入   json   oid   col   檔案   sys   緩衝   輸出   stat   

 1 package com; 2  3 import java.io.*; 4  5 /** 6  * Hello world! 7  * 8  */ 9 public class ReadFile10 {11     public static void main( String[] args ) {12         String fileName = "F:/aa.json";13         String result = readFile(fileName);14         System.out.println(result);15     }16     //FileReader讀取字元,可能出現亂碼,FileInputStream讀取位元組,不會出現亂碼17     private static String readFile(String fileName) {18         StringBuilder sb = new StringBuilder();19         try {20             // 建立FileReader對象,用來讀取字元流21             FileReader fileReader =new FileReader(fileName);22             // 緩衝指定檔案的輸入23             BufferedReader bufferedReader = new BufferedReader(fileReader);24             // 建立FileWriter對象,用來寫入字元流25             FileWriter fileWriter = new FileWriter("F:/response2.json");26             // 將緩衝對檔案的輸出27             BufferedWriter bw = new BufferedWriter(fileWriter);28             String eachLine;29             //每次讀取一行30             while ((eachLine=bufferedReader.readLine())!=null){31                 sb.append(eachLine);32                 //寫一行,需要換行,否則列印在一行,需要close,否則為空白33                 bw.write(eachLine);34                 bw.newLine();35             }36             bw.close();37 38         } catch (FileNotFoundException e) {39             e.printStackTrace();40         } catch (IOException e) {41             e.printStackTrace();42         }43         return sb.toString();44     }45     private String readFileByByte(String fileName){46         FileInputStream inputStream =new FileInputStream()47         return48     }49 }

 

Java讀取檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.