File read/write

    0

    0

    Vinod Pahuja

    Swiss Army Knife

    String line; while ((line = scanner.nextLine()) != null) { System.out.println(line); }

    Files.

    //--------------------
    // file write
    //--------------------
    
    //ref : https://www.baeldung.com/java-write-byte-array-file
    
    try (FileOutputStream fos = new FileOutputStream("pathname")) { fos.write(myByteArray); }
    java.io.FileOutputStream os = new java.io.FileOutputStream("d:/para.class"); os.write(arg1); os.close()
    Files.write(outputFile.toPath(), dataForWriting);
    
    //--------------------
    // file read
    //--------------------
    // ref : https://www.baeldung.com/java-csv-file-array
    
    // 1.
    List<List<String>> records = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader("book.csv"))) {
        String line;
        while ((line = br.readLine()) != null) {
            String[] values = line.split(COMMA_DELIMITER);
            records.add(Arrays.asList(values));
        }
    }
    
    // 2.
    List<List<String>> records = new ArrayList<>();
    try (Scanner scanner = new Scanner(new File("book.csv"));) {
        while (scanner.hasNextLine()) {
            records.add(getRecordFromLine(scanner.nextLine()));
        }
    }
    
    private List<String> getRecordFromLine(String line) {
        List<String> values = new ArrayList<String>();
        try (Scanner rowScanner = new Scanner(line)) {
            rowScanner.useDelimiter(COMMA_DELIMITER);
            while (rowScanner.hasNext()) {
                values.add(rowScanner.next());
            }
        }
        return values;
    }
    
    // ref : https://www.baeldung.com/java-file-to-arraylist
    
    // 1.
    ArrayList<String> result = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
        while (br.ready()) {
            result.add(br.readLine());
        }
    }
    
    // 2.
    List<String> result = Files.readAllLines(Paths.get(filename));
    
    // ref : https://www.baeldung.com/reading-file-in-java
    
    // 1.
    Path path = Paths.get("src/test/resources/fileTest.txt");
    BufferedReader reader = Files.newBufferedReader(path);
    String line = reader.readLine();
    
    // 2.
    private static String read(java.lang.String file) throws Exception {
      Path path = Paths.get(<>.class.getClassLoader().getResource(file).toURI());
      try (Stream<String> lines = Files.lines(path)) {
      return lines.collect(Collectors.joining("\n"));
      }
    }
    
    // 3.
    Path path = Paths.get(getClass().getClassLoader().getResource("fileTest.txt").toURI());
    Stream<String> lines = Files.lines(path);
    String data = lines.collect(Collectors.joining("\n"));
    lines.close();
    
    // 4.
    String file = "src/test/resources/fileTest.txt";
    Scanner scanner = new Scanner(new File(file));
    scanner.useDelimiter(" ");
    scanner.hasNext();
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.