1.流的分类
提示
/** * 书写文件内容 */ public static void writeWord(String str) throws IOException { try ( FileOutputStream fileOutputStream = new FileOutputStream(URLDecoder.decode(path, "UTF-8")+"static\system.txt"); // FileOutputStream fileOutputStream = new FileOutputStream("/usr/java/myfile/system.txt"); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8"); PrintWriter out = new PrintWriter(outputStreamWriter); ){ out.write(str); out.flush(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } }
/** * 读取文件内容 */ public static String readWord () { try ( FileInputStream fileInputStream = new FileInputStream(URLDecoder.decode(path, "UTF-8")+"static\system.txt"); // FileInputStream fileInputStream = new FileInputStream("/usr/java/myfile/system.txt"); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8"); BufferedReader br = new BufferedReader(inputStreamReader); ){ String line = null; StringBuffer sBuffer = new StringBuffer(); while((line = br.readLine())!=null){ sBuffer.append(line); } return sBuffer.toString(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } }