Java sample code 02

I communicated the source posted in SAMPLE GALLERY and added an import statement.

2.FileIO --Write to text file --Insert line breaks in text files --Reading a text file --Loading web page content --Copy file to another file --Specify the character code and read the text file (measures against garbled characters) --Write a string to a text file --Read a text file string

2-1. Write to a text file

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");//The file to be written is specified here.
            out.write(data);//I am writing characters here.
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }

    }

}

2-2. Insert a line break in the text file

import java.io.*;

public class SampleOutputStream {

    private static final String NEW_LINE = "\n";//Line feed code constant

    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);//The first line
            out.write(NEW_LINE.getBytes());//new line
            out.write(data);//2nd line
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
        //Get the absolute path of the file
        File file = new File("OutputText.txt");
        String str = file.getAbsolutePath();
        System.out.println("pass : " + str);

    }

}

2-3. Reading a text file

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;

            //I'm reading a file.
            //read()When the method goes to the end-Because it returns 1.-Turn the loop until 1 is returned.
            while ((i = in.read()) != -1) {
                char character = (char) i;
                System.out.print(character);//Output to console
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
        }

    }

}

2-4. Loading the contents of the web page

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 settings
        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/");//Specify a web page.
        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. Copy file to another file

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");//Specify the copy source file
            out = new FileOutputStream("OutputText.txt");//Specify the copy destination file
            inChannel = in.getChannel();
            outChannel = out.getChannel();
            inChannel.transferTo(0, inChannel.size(), outChannel);//Copy file
        } 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. Specify the character code and read the text file (measures against garbled characters)

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");//The character code is specified here.
            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. Write a character string to a text file

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. Read the character string of the text file

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 sample code 02
Java sample code 03
Java sample code 04
Java sample code 01
Digital signature sample code (JAVA)
Java parallelization code sample collection
Script Java code
Java code TIPS
[Java] Generics sample
Selenium sample (Java)
Java GUI sample
Java 9 new features and sample code
Java character code
Sample code using Minio from Java
Sample code collection for Azure Java development
Apache beam sample code
[Java] Holiday judgment sample
[Java] logback slf4j sample
[Java] Explanation of Strategy pattern (with sample code)
Java test code method collection
[Windows] Java code is garbled
Java
Java in Visual Studio Code
Java standard log output sample
Write Java8-like code in Java8
Sample code for log output by Java + SLF4J + Logback
Java
Selenium Sample Reservation Form (Java)
Sample code to parse date and time with Java SimpleDateFormat
Guess the character code in Java
Code Java from Emacs with Eclim
Java Spring environment in vs Code
Java 15 implementation and VS Code preferences
[Java] Boilerplate code elimination using Lombok
Java build with mac vs code
Arbitrary string creation code by Java
Execute packaged Java code with commands
A simple sample callback in Java
Java source code reading java.lang.Math class
[Java] Boilerplate code elimination using Lombok 2
BloomFilter description and implementation sample (JAVA)
[Java] Date period duplication check sample
EXCEL file update sample with JAVA
Java development environment (Mac, VS Code)
[Android] Convert Android Java code to Kotlin
Sample vending machine made in Java
Basic structure of Java source code
How to manage Java code automatically generated by jOOQ & Flyway sample
Sample code to call the Yahoo! Local Search API in Java
Sample code that uses the Mustache template engine JMustache in Java
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Prepare Java development environment with VS Code
[Java] Module
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Sample code for Singleton implementation using enum