Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read a short text file into byte array

Posted on 2004-10-06
3
Medium Priority
?
316 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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

782 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