How to read file char by char using FileInputStream() in Java

1 Answer

0 votes
package javaapplication1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class JavaApplication1 {

    public static void main(String[] args) {

        File file = new File("d://data.txt");
        int ch;

        try {
            StringBuilder sb = new StringBuilder("");
            try (FileInputStream fis = new FileInputStream(file)) {
                while ((ch = fis.read()) != -1) {
                    sb.append((char) ch);
                }
                System.out.println(sb);
            }

        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }
}

/*
      
run:

java c c++ c# php
  
 */

 



answered Nov 12, 2016 by avibootz

Related questions

1 answer 200 views
1 answer 267 views
1 answer 114 views
114 views asked Feb 3, 2023 by avibootz
1 answer 180 views
180 views asked Jun 6, 2018 by avibootz
...