Solved

Read a short text file into byte array

Posted on 2004-10-06
3
294 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
Comment Utility
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
Comment Utility
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
Comment Utility
OK, I was missing import java.io.*;
It works now
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 59
bunnyEars2 challenge 6 66
word0 challenge 4 52
get weblogic logged in user in java 2 39
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now