2013-10-26 27 views

回答

-1

下面的行可能是你想要的。

public static String readFromFile(String filePath){ 
    String total=""; 
    File file=new File(filePath); 
     try { 
     String encoding="GBK"; 
       if(file.isFile() && file.exists()){ //判斷文件是否存在 
       InputStreamReader read = new InputStreamReader(
         new FileInputStream(file),encoding);//考慮到漢子編碼格式 
       BufferedReader bufferedReader = new BufferedReader(read); 
       String lineTxt = null; 
       while((lineTxt = bufferedReader.readLine()) != null){ 
        total+=lineTxt; 
       } 
       read.close(); 
     }else if(!file.exists()) 
     { 
     file.createNewFile(); 
     } 
     } catch (Exception e) { 
     System.out.println("讀取文件內容出錯"); 
     e.printStackTrace(); 
     } 
    return total; 
    } 
+0

如果你想讀取系統文件,你可能會學習一些不良的linux,你可以通過使用這些代碼來獲得根存儲:Process process = null; try {0} {0} {0} process = Runtime.getRuntime()。exec(「su」); process.waitFor(); (例外e){ } e.printStackTrace(); } finally { process.destroy(); } – orzangleli

相關問題