Home>

Recently started development with Java.

Some folders contain multiple files.
I want to write code that opens one file at a time from this folder and reads the contents into a buffer or array.
What class and method should I use at this time?
I want to write as easily as possible.

If i look online,
① Create an object of File class and pass the folder path
② Open the file with FileInputStream
③Read to buffer with BufferedReader
I think this is the flow.
However, when I try to write code, I get a compile error saying that FileInputStream cannot be converted to BufferedReader.
How should I write the code?

  • Answer # 1

    Simple code to read line by line

    The file is a "text file" with the character set UTF-8, and is an example of reading line by line.

    List<String>lines = Files.readAllLines (Paths.get ("./ sample.txt"), StandardCharsets.UTF_8);
    Easy code to read in bulk

    The file is a "text file" with the character set UTF-8.

    byte [] bytes = Files.readAllBytes (Paths.get ("./ sample.txt"));
    String all = new String (bytes, StandardCharsets.UTF_8);
    How to get multiple files

    Example of outputting a list of files excluding directories.

    try (Stream<Path>paths = Files.list (Paths.get ("d: /")))) {
        paths.filter (path->path.toFile (). isFile ()). forEach (System.out :: println);
    } catch (IOException ex) {
        ex.printStackTrace (System.err);
    }

  • Answer # 2

    Use BufferedInputStream instead of BufferedReader.
    Also, I think that adding more codes will give you more answers.

  • Answer # 3

    FileInputStream fis = new FileInputStream (new File (file));
    InputStreamReader isr = new InputStreamReader (fis);
    BufferedReader br = new BufferedReader (isr);
    It is the order of

    .

  • Answer # 4

    This program keeps all file information in the folder in ArrayList
    There is also a method for writing

    import java.io. *;
    import java.util. *;
    import java.nio.file. *;
    class LA {
    static ArrayList<Point>p = new ArrayList<Point>();
    public static void main (String [] args) {
    Path path = Paths.get ("C: \\ file \\ files \\ file \\ Java \\");// Take it out and put it in the Point class and put the Point class together in p of ArrayList
    try {
    Files.walk (path) .filter (s->s.toFile (). IsFile ()). ForEach (c->Freserve (c));
    } catch (IOException ex) {
        ex.printStackTrace (System.err);
    }
    // WriteFile ();// This method will copy
    }
    static void Freserve (Path c) {
    Point kel = new Point ();
    kel.fname = c;
    String sd = c.toString ();
    try (FileInputStream fis = new FileInputStream (new File (sd));) {
    int data = 0;
    while ((data = fis.read ())! =-1) {
    kel.s.add (data);
    }
    } catch (Exception e) {}
    p.add (kel);
    }
    static void WriteFile () {
    String direct = "C: \\ Java \\ Java \\ Java \\";// This folder is created and all the files are copied into it
    File w2 = new File (direct);
    w2.mkdirs ();
    for (int c = 0;c<p.size ();c ++) {
    Point q = p.get (c);
    String s = q.fname.toString ();
    int d = s.lastIndexOf ("\\");
    String s2 = s.substring (d + 1, s.length ());
    s2 = direct + s2;
    try (FileOutputStream fis = new FileOutputStream (new File (s2));) {
    int data = 0;
    for (int d3: q.s) {
    fis.write (d3);
    }
    } catch (Exception e) {}
    }
    }
    }
    class Point {
    ArrayList<Integer>s = new ArrayList<>();
    Path fname;
    }