乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      JAVA讀寫文件,如何避免中文亂碼 - 52齋347 - JavaEye技術(shù)網(wǎng)站

       Ethan的博客 2011-03-26
      最近在做HTML靜態(tài)生成,需要從硬盤上把模版文件的內(nèi)容讀出來。然后,替換相關(guān)標(biāo)簽寫到指定的文件中。無論是讀寫,都遇到了中文亂碼問題。試過多種方法,發(fā)現(xiàn)下面一種可以避免中文亂碼。(無論讀取還是寫入一定要進(jìn)行編碼轉(zhuǎn)換。)

      1、JAVA讀取文件,避免中文亂碼。

      /**
        * 讀取文件內(nèi)容
        *
        * @param filePathAndName
        *            String 如 c:\\1.txt 絕對路徑
        * @return boolean
        */
      public static String readFile(String filePathAndName) {
        String fileContent = "";
        try { 
         File f = new File(filePathAndName);
         if(f.isFile()&&f.exists()){
          InputStreamReader read = new InputStreamReader(new FileInputStream(f),"UTF-8");
          BufferedReader reader=new BufferedReader(read);
          String line;
          while ((line = reader.readLine()) != null) {
           fileContent += line;
          }  
          read.close();
         }
        } catch (Exception e) {
         System.out.println("讀取文件內(nèi)容操作出錯(cuò)");
         e.printStackTrace();
        }
        return fileContent;
      }

      2、JAVA寫入文件,避免中文亂碼。

      public static void writeFile(String filePathAndName, String fileContent) {
        try {
         File f = new File(filePathAndName);
         if (!f.exists()) {
          f.createNewFile();
         }
         OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"UTF-8");
         BufferedWriter writer=new BufferedWriter(write);  
         //PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filePathAndName)));
         //PrintWriter writer = new PrintWriter(new FileWriter(filePathAndName));
         writer.write(fileContent);
         writer.close();
        } catch (Exception e) {
         System.out.println("寫文件內(nèi)容操作出錯(cuò)");
         e.printStackTrace();
        }
      }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多