The thing is that the new girl in the next group could not write the generated TXT file, so the leader asked me to guide it. So I taught her to write several methods and told her about the try-with-resourse technique and told her to read JDK source code and brush less boiling point. If you don’t understand JDK source code, you can see the article on……

// Generate file path String path="C:\ Users\\ Desktop\\"; // fileName String fileName="a.txt"; List<String> list=new ArrayList(); list.add("sister:"+"\n"); list.add("bro"); File file =new File(path+fileName);Copy the code

1.FileWrite

try{ FileWriter fileWritter = new FileWriter(file); for(String ele:list){ fileWritter.write(ele); } // Manually close filewritter.close (); }catch (Exception e){ e.printStackTrace(); }Copy the code

2. Specify the character set and automatically close the stream

try(BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"))){
   for(String ele:list){   out.write(ele);
}}catch (Exception e){   
 e.printStackTrace();
}
   
Copy the code

3. The Files encapsulation method can specify the character set

* @param path * The path to the file: For example, d:\\a. xt * @param lines * an object to iterate over the char sequences: List * @param cs * the charset to use for encoding character set Charset.forName("utf-8")Files.write(Paths.get(path+fileName),list,Charset.forName("utf-8"));Copy the code

We all know that things like streams and connections need to be turned off as soon as they are turned on or they waste resources. Another important reason is that if you don’t close the stream, the first time it might be fine, but over time, if you try to open the stream again you will be told that the file is occupied or the system will crash. For this reason, we would normally give some form of manual close, which is to give some form of finnly close after use, or to give some form of finnly close, which is try-catch-finnly. Since jdK7 with try-with-resource we no longer need to finnly, the program will automatically turn off stream, but you have to write it as it is and it will turn off for you.

4.try-with-resource

The try-with resource statement ensures that each resource is closed at the end of a statement. Any implemented object java.lang.AutoCloseable

All implemented objects, java.io.Closeable, can be used as resources. But the resource life must be enclosed in parentheses after the try, just like **2. Specify the character set and automatically close the stream **. Write like that!

This way we don’t have to close the stream manually.

After listening to my warm explanation and hand touch hand friendly guidance, sister raised her head sincerely said to me: “thank you”, I pretended to be calm said: “Nothing, there are technical problems to find me.” I repressed the joy in the heart of the stride meteor towards my station, after sitting down to find the communication has a sister’s friend application……

Reading Reference:

Docs.oracle.com/javase/tuto…