将字符串写入文件与读取文件

 一,读取文件内容

            File file = new File(filePath);
            if(file.isFile() && file.exists()) {
                
                StringBuilder result = new StringBuilder();
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
                BufferedReader br = new BufferedReader(isr);
                String lineTxt = null;
                while ((lineTxt = br.readLine()) != null) {
                    result.append(lineTxt);
                }
                br.close();
            }
            
                

二,将字符串写入文件

            File file = new File(filePath);
			if(!file.exists()){
				file.createNewFile();
			}
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
            //content为字符串内容
	        osw.write(content);
	        osw.close();

 

你可能感兴趣的:(笔记)