使用java 程序創建格式為utf-8文件的方法(寫入和讀取json文件)


使用java 程序創建格式為utf-8文件的方法:
  
try{
            File file=new   File("C:/11.jsp");  
            OutputStream out=new FileOutputStream(file);
            BufferedWriter   rd   =   new BufferedWriter(new OutputStreamWriter(out,"utf-8"));
            rd.write("333333333333中國");
            rd.close();
            out.close();
        }catch(IOException e){
            e.printStackTrace();
        }

 

//判斷文件是否存在

//(獲取服務器的config下的路徑)
File fileExit = new File(this.getClass().getClassLoader().getResource(path).toURI().getPath());
if(!fileExit.exists()){
return fileContent;
}

//(獲取webroot下文件路徑)

ServletContext sctx = ServletActionContext.getServletContext();
String path = sctx.getRealPath("/");// 服務器文件路徑
File filePath=new File("uploadfiles/temp/"+"2016-11-09");//拼接文件路徑



String fileContent = "";
InputStreamReader file = new InputStreamReader(
IGetEshopid.class.getClassLoader()
.getResourceAsStream(path), "UTF-8");
BufferedReader reader = new BufferedReader(file);

String tempString = null;
while ((tempString = reader.readLine()) != null) {
fileContent += tempString;
}
reader.close();
return fileContent;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM