IO示例练习

import org.junit.Test;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class App {

    @Test
    public void writeNewContent() throws IOException {
        File[] files = new File("E:/backup").listFiles((dir, name) -> new File(dir, name).isFile() && name.endsWith("log"));
        if (files == null || files.length == 0) return;
        FileWriter writer = new FileWriter(files[0]);
        String str = null;
        writer.write(str);
        writer.close();
        String pathname = null;
        files[0].renameTo(new File(pathname));
    }

    @Test
    public void downResource() throws IOException {
        File[] files = new File("E:/backup").listFiles((dir, name) -> new File(dir, name).isFile() && name.endsWith("log"));
        if (files == null || files.length == 0) return;
        String spec = null;
        URLConnection connection = new URL(spec).openConnection();
        connection.connect();
        BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
        BufferedOutputStream _stream = new BufferedOutputStream(new FileOutputStream(files[0]));
        byte[] b = new byte[1024];
        while (stream.read(b) != -1) {
            _stream.write(b,0,b.length);
        }
        _stream.close();
        stream.close();
        String pathname = null;
        files[0].renameTo(new File(pathname));
    }
}

你可能感兴趣的:(io,java)