2.FileIO --Écrire dans un fichier texte --Insérez un saut de ligne dans le fichier texte --Lecture d'un fichier texte --Chargement du contenu de la page Web --Copier le fichier dans un autre fichier --Spécifier un code de caractère et lire un fichier texte (mesures contre les caractères déformés)
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SampleOutputStream {
    public static void main(String[] args) throws IOException {
        String text = "Hello World.";
        byte[] data = text.getBytes();
        OutputStream out = null;
        try {
            out = new FileOutputStream("OutputText.txt");//Le fichier à écrire est spécifié ici.
            out.write(data);//J'écris des personnages ici.
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
import java.io.*;
public class SampleOutputStream {
    private static final String NEW_LINE = "\n";//Constante de code de saut de ligne
    public static void main(String[] args) throws IOException {
        String text = "Hello World.";
        byte[] data = text.getBytes();
        OutputStream out = null;
        try {
            out = new FileOutputStream("OutputText.txt");
            out.write(data);//La première ligne
            out.write(NEW_LINE.getBytes());//nouvelle ligne
            out.write(data);//2e ligne
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
        //Obtenez le chemin absolu du fichier
        File file = new File("OutputText.txt");
        String str = file.getAbsolutePath();
        System.out.println("pass : " + str);
    }
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class SampleInputStream {
    public static void main(String[] args) throws IOException {
        InputStream in = null;
        try {
            in = new FileInputStream("InputText.txt");
            int i = -1;
            //Je lis un fichier.
            //read()Quand la méthode va jusqu'au bout-Parce qu'il renvoie 1.-Tournez la boucle jusqu'à ce que 1 soit renvoyé.
            while ((i = in.read()) != -1) {
                char character = (char) i;
                System.out.print(character);//Sortie vers la console
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class SampleInputStream {
    public static void main(String[] args) throws IOException {
        //paramètres du proxy
        System.setProperty("http.proxyHost","test.proxy.com");
        System.setProperty("http.proxyPort","8081");
        System.setProperty("https.proxyHost","test.proxy..com");
        System.setProperty("https.proxyPort","8081");
        System.setProperty("http.nonProxyHosts","localhost|*.nonproxy.com");
        URL url = new URL("https://www.google.co.jp/");//Spécifiez une page Web.
        InputStream in = null;
        try {
            in = url.openStream();
            int i = -1;
            while ((i = in.read()) != -1) {
                char character = (char) i;
                System.out.print(character);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class SampleOutputStream {
    public static void main(String[] args) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            in = new FileInputStream("InputText.txt");//Spécifiez le fichier source de la copie
            out = new FileOutputStream("OutputText.txt");//Spécifiez le fichier de destination de la copie
            inChannel = in.getChannel();
            outChannel = out.getChannel();
            inChannel.transferTo(0, inChannel.size(), outChannel);//Copier un fichier
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (outChannel != null) {
                outChannel.close();
            }
            if (inChannel != null) {
                inChannel.close();
            }
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        }
    }
}
import java.io.*;
public class SampleInputStream {
    public static void main(String[] args) throws IOException {
        InputStream in = null;
        InputStreamReader inr = null;
        try {
            in = new FileInputStream("InputText.txt");
            inr = new InputStreamReader(in, "UTF-8");//Le code de caractère est spécifié ici.
            int i = -1;
            while ((i = inr.read()) != -1) {
                char character = (char) i;
                System.out.print(character);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (inr != null) {
                inr.close();
            }
            if (in != null) {
                in.close();
            }
        }
    }
}
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class SampleOutputStream {
    public static void main(String[] args) throws IOException {
        FileWriter out = null;
        try {
            out = new FileWriter("OutputText.txt");
            out.write("Hello World.");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class SampleBufferedReader {
    public static void main(String[] args) throws IOException {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader("InputText.txt");
            br = new BufferedReader(fr);
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                br.close();
            }
            if (fr != null) {
                fr.close();
            }
        }
    }
}
        Recommended Posts