Java-Beispielcode 02

Ich habe die in SAMPLE GALLERY veröffentlichte Quelle mitgeteilt und eine Importanweisung hinzugefügt.

2.FileIO

2-1. In Textdatei schreiben

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");//Die zu schreibende Datei wird hier angegeben.
            out.write(data);//Ich schreibe hier Charaktere.
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }

    }

}

2-2 Fügen Sie einen Zeilenumbruch in die Textdatei ein

import java.io.*;

public class SampleOutputStream {

    private static final String NEW_LINE = "\n";//Konstante des Zeilenvorschubcodes

    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);//Die erste Zeile
            out.write(NEW_LINE.getBytes());//Neue Zeile
            out.write(data);//2. Zeile
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
        //Holen Sie sich den absoluten Pfad der Datei
        File file = new File("OutputText.txt");
        String str = file.getAbsolutePath();
        System.out.println("pass : " + str);

    }

}

2-3. Lesen einer Textdatei

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;

            //Ich lese eine Datei.
            //read()Wenn die Methode zu Ende geht-Weil es 1 zurückgibt.-Drehen Sie die Schleife, bis 1 zurückgegeben wird.
            while ((i = in.read()) != -1) {
                char character = (char) i;
                System.out.print(character);//Ausgabe an die Konsole
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
        }

    }

}

2-4 Laden des Inhalts der Webseite

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 {
        //Proxy-Einstellungen
        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/");//Geben Sie eine Webseite an.
        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();
            }
        }

    }

}

2-5. Datei in eine andere Datei kopieren

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");//Geben Sie die Quelldatei an
            out = new FileOutputStream("OutputText.txt");//Geben Sie die Kopierzieldatei an
            inChannel = in.getChannel();
            outChannel = out.getChannel();
            inChannel.transferTo(0, inChannel.size(), outChannel);//Datei kopieren
        } 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();
            }
        }

    }

}

2-6 Geben Sie den Zeichencode an und lesen Sie die Textdatei (Maßnahmen gegen verstümmelte Zeichen)

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");//Der Zeichencode wird hier angegeben.
            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();
            }
        }

    }

}

2-7. Schreiben Sie eine Zeichenfolge in eine Textdatei

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();
            }
        }

    }

}

2-8. Lesen Sie die Zeichenfolge der Textdatei

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

Java-Beispielcode 02
Java-Beispielcode 03
Java-Beispielcode 04
Java-Beispielcode 01
Beispielcode für elektronische Signatur (JAVA)
Java Parallel Code Sample Collection
Führen Sie Java-Code skriptweise aus
Java-Code-TIPPS
[Java] Generics-Beispiel
Selenprobe (Java)
Java GUI Beispiel
Java 9 neue Funktionen und Beispielcode
Java-Zeichencode
Beispielcode mit Minio aus Java
Apache Beam Beispielcode
[Java] Beispiel für ein Urlaubsurteil
[Java] logback slf4j Beispiel
Sammlung von Java-Testcode-Methoden
[Windows] Java-Code ist verstümmelt
Java
Java mit Visual Studio Code
Beispiel für eine Java-Standardprotokollausgabe
Schreiben Sie Java8-ähnlichen Code in Java8
Beispielcode für die Protokollausgabe von Java + SLF4J + Logback
Java
Selenium Musterbuchungsformular (Java)
Beispielcode zum Parsen von Datum und Uhrzeit mit Java SimpleDateFormat
Errate den Zeichencode in Java
Code Java von Emacs mit Eclim
Java Spring-Umgebung in vs Code
[Java] Eliminierung des Kesselplattencodes mit Lombok
Erstellen Sie Java mit Mac vs Code
Beliebiger Code zum Erstellen von Zeichenfolgen durch Java
Führen Sie gepackten Java-Code mit Befehlen aus
Ein einfaches Beispiel für Rückrufe in Java
Java-Quellcode zum Lesen der Klasse java.lang.Math
[Java] Eliminierung des Kesselplattencodes mit Lombok 2
BloomFilter-Beschreibungs- und Implementierungsbeispiel (JAVA)
[Java] Beispiel für eine Überprüfung des Datumszeitraums
Beispiel für eine EXCEL-Dateiaktualisierung mit JAVA
Java-Entwicklungsumgebung (Mac, VS Code)
[Android] Konvertieren Sie Android Java-Code in Kotlin
Beispielautomat mit Java
Grundstruktur des Java-Quellcodes
So verwalten Sie Java-Code, der automatisch von jOOQ & Flyway generiert wird
Beispielcode zum Aufrufen der Yahoo! Local Search API in Java
Beispielcode mit JMustache, der Moustache-Vorlagen-Engine in Java
Java lernen (0)
Java studieren ―― 3
[Java] -Array
Java geschützt
[Java] Anmerkung
Bereiten Sie die Java-Entwicklungsumgebung mit VS Code vor
[Java] Modul
Java studieren ―― 9
Java Scratch Scratch
Java-Tipps, Tipps
Beispielcode für die Singleton-Implementierung mit enum