Solved

Read a short text file into byte array

Posted on 2004-10-06
3
298 Views
Last Modified: 2006-11-17
How to read a short text file (like "Hello World") in byte array?
0
Comment
Question by:edi02
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
ThummalaRaghuveer earned 50 total points
ID: 12245076
Here is one of the crudest method

public class Reader {
      
      public static void main(String args[]) {
            try {
                  FileInputStream fip = new FileInputStream(new File("abc.txt"));
                  byte a;
                  int index = 0;
                  byte[] out = new byte[5000];
            
                  do
                  {
                        
                        a = (byte) fip.read();
                        
                        index++;
                        
                        out[index-1] = a;
                  }while(a != -1);
                  
                  String a1 = new String(out,0,index-1);
                  System.out.println(a1);
                  
            } catch (FileNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
      }

}
0
 

Author Comment

by:edi02
ID: 12245246
THis is what I get in javac:


C:\My Books\Java examples>javac Reader.java
Reader.java:5: cannot resolve symbol
symbol  : class FileInputStream
location: class Reader
               FileInputStream fip = new FileInputStream(new File("abc.txt"));
               ^
Reader.java:5: cannot resolve symbol
symbol  : class FileInputStream
location: class Reader
               FileInputStream fip = new FileInputStream(new File("abc.txt"));
                                         ^
Reader.java:5: cannot resolve symbol
symbol  : class File
location: class Reader
               FileInputStream fip = new FileInputStream(new File("abc.txt"));
                                                             ^
Reader.java:23: cannot resolve symbol
symbol  : class FileNotFoundException
location: class Reader
          } catch (FileNotFoundException e) {
                   ^
Reader.java:26: cannot resolve symbol
symbol  : class IOException
location: class Reader
          } catch (IOException e) {
                   ^
5 errors
0
 

Author Comment

by:edi02
ID: 12245248
OK, I was missing import java.io.*;
It works now
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question