Didart

Write to File

Jan 25th, 2023 (edited)
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. package StreamsFilesAndDirectories4;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.*;
  7.  
  8. public class WriteToFile {
  9.     public static void main(String[] args) throws IOException {
  10.  
  11.         String pathIn = "input.txt";
  12.         String pathOut = "output.txt";
  13.  
  14.         Set<Character> forbiddenSymbols = new HashSet<>();
  15.         Collections.addAll(forbiddenSymbols, '.', ',', '!', '?');
  16.  
  17.        try (FileInputStream in = new FileInputStream(pathIn);
  18.             FileOutputStream out = new FileOutputStream(pathOut)) {
  19.  
  20.            int oneByte = in.read();
  21.            while (oneByte >= 0) {
  22.                char myByteAsChar = (char) oneByte;
  23.                if (!forbiddenSymbols.contains(myByteAsChar)) {
  24.                    out.write(oneByte);
  25.                }
  26.                oneByte = in.read();
  27.            }
  28.        } catch (IOException e){
  29.            throw  new RuntimeException(e);
  30.        }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment