Posted By

kvaso on 05/01/10


Tagged


Versions (?)

citanie znakov zo suboru


 / Published in: Java
 

  1. import java.io.BufferedInputStream;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5.  
  6. /*
  7.  * To change this template, choose Tools | Templates
  8.  * and open the template in the editor.
  9.  */
  10.  
  11. public class Main {
  12.  
  13. /**
  14.   * This method reads contents of a file and print it out
  15.   */
  16. public void readFromFile(String filename) {
  17.  
  18. BufferedInputStream bufferedInput = null;
  19. byte[] buffer = new byte[1024];
  20.  
  21. try {
  22.  
  23. //Construct the BufferedInputStream object
  24. bufferedInput = new BufferedInputStream(new FileInputStream(filename));
  25.  
  26. int bytesRead = 0;
  27.  
  28. //Keep reading from the file while there is any content
  29. //when the end of the stream has been reached, -1 is returned
  30. while (bufferedInput.available() != 0) {
  31. System.out.println(bufferedInput.read() + "");
  32. //Process the chunk of bytes read
  33. //in this case we just construct a String and print it out
  34. }
  35.  
  36. } catch (FileNotFoundException ex) {
  37. ex.printStackTrace();
  38. } catch (IOException ex) {
  39. ex.printStackTrace();
  40. } finally {
  41. //Close the BufferedInputStream
  42. try {
  43. if (bufferedInput != null) {
  44. bufferedInput.close();
  45. }
  46. } catch (IOException ex) {
  47. ex.printStackTrace();
  48. }
  49. }
  50. }
  51.  
  52. /**
  53.   * @param args the command line arguments
  54.   */
  55. public static void main(String[] args) {
  56. new Main().readFromFile("myFile.txt");
  57. }
  58. }

Report this snippet  

You need to login to post a comment.