[Java] Stream processing

What is a stream?

Base class Input stream Output stream
Character stream Reader Writer
Binary stream InputStream OutputStream

File writing

//Execution time./data.Record in log
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
//import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;

public class Main {
  public static void main(String[] args) {
    try (var writer = Files.newBufferedWriter(Paths.get("./data.log"))) {
    //File addition
    //try (var writer = Files.newBufferedWriter(Paths.get("./data.log"), StandardOpenOption.APPEND)) {
    writer.write(LocalDateTime.now().toString());
    //When unifying line feed characters
    //write(LocalDateTime.now().toString()+"\r\n");
      writer.newLine();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

File input

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    try (var reader = Files.newBufferedReader(Paths.get("./sample.txt"))) {
      var line = "";
      while ((line = reader.readLine()) != null) {
        //The contents of the file
        System.out.println(line);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Reading and writing binary files

//input.Read png with BufferedInputStream
//Output the result with BufferedOutputStream.Output to png
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    try (
        var in = new BufferedInputStream(
          new FileInputStream("./input.png "));
        var out = new BufferedOutputStream(
          new FileOutputStream("./output.png "))) {
      var data = -1;
      while ((data = in.read()) != -1) {
        out.write((byte) data);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Object serialization

//article.Article class on ser(Serializable class)Save
//Reads the byte array from the file and outputs the contents of the deserialized public key cryptographic object
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main {
  public static void main(String[] args) {
    final var file = "./article.ser";
    try (var out = new ObjectOutputStream(new FileOutputStream(file))) {
      out.writeObject(new Article("Java 11 changes and new API",
        "https://codezine.jp/article/corner/751", false));
    } catch (IOException e) {
      e.printStackTrace();
    }

    try (var in = new ObjectInputStream(new FileInputStream(file))) {
      var a = (Article)in.readObject();
      System.out.println(a);
    } catch (ClassNotFoundException | IOException e) {
      e.printStackTrace();
    }
  }
}

Recommended Posts

[Java] Stream processing
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
[JAVA] Stream type
Java thread processing
Java string processing
Basic processing flow of java Stream
[Java] Multi-thread processing
Stream intermediate processing
Java Stream termination
Java 9 Optional :: stream
java iterative processing
[Java] Stream Collectors notes
[Java] Stream API-Stream generation
[Java] Stream API / map
Java8 Stream API practice
JAVA constructor call processing
Java random, various processing
Java8 Stream Rough Summary
List processing to understand with pictures --java8 stream / javaslang-
[Java] Multi-thread processing --Exclusive control
[Java11] Stream Summary -Advantages of Stream-
Java Stream API cheat sheet
Stream processing of Java 8 can be omitted so far!
Java Stream API in 5 minutes
Java8 stream, lambda expression summary
Java
[Java] Timer processing implementation method
Java Stream cannot be reused.
Measured parallel processing in Java
[Java] Introduction to Stream API
Use Redis Stream in Java
Java
[Java11] Stream Usage Summary -Basics-
Java application for beginners: stream
[Java] Stream API intermediate operation
Summary of java error processing
List processing to understand with pictures --java8 stream / javaslang --bonus
[Java 8] Duplicate deletion (& duplicate check) with Stream
[Java] Stream (filter, map, forEach, reduce)
[java8] To understand the Stream API
About Lambda, Stream, LocalDate of Java8
[Introduction to Java] About Stream API
Date processing in Java (LocalDate: Initialization)
Delegate some Java processing to JavaScript
[Java] Loop processing and multiplication table
Run node.js from android java (processing)
[Java] Element existence check with Stream
[Processing × Java] How to use variables
I tried using Java8 Stream API
Server processing with Java (Introduction part.1)
Surprisingly deep Java list inversion-Stream processing
About file copy processing in Java
Notes on Android (java) thread processing
Deleting files using recursive processing [Java]
Java 8 ~ Stream API ~ to start now
[Processing × Java] How to use arrays
[Java] Processing time measurement method memo
Java array / list / stream mutual conversion list
Java8 list conversion with Stream map
Do you use Stream in Java?