尝试

欢马劈雪     最近更新时间:2020-01-02 10:19:05

538
Try-with-resources块可简化错误处理并使代码更简洁

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;/**from n  o  w  j  a  v  a . c o m**/

import java.net.URI;

import java.net.URISyntaxException;

import java.nio.charset.Charset;

import java.nio.file.Files;

import java.nio.file.Paths;


public class Main {


    public static void main(String[] args) {

        try (BufferedReader inputReader = Files.newBufferedReader(

                        Paths.get(new URI("file:///C:/home/docs/users.txt")),

                        Charset.defaultCharset());

                BufferedWriter outputWriter = Files.newBufferedWriter(/** 时 代 J a v a - N o w J a v a . c o m 提 供 **/

                        Paths.get(new URI("file:///C:/home/docs/users.bak")),

                        Charset.defaultCharset())) {

            String inputLine;

            while ((inputLine = inputReader.readLine()) != null) {

                outputWriter.write(inputLine);

        
展开阅读全文