Solved

Read a short text file into byte array

Posted on 2004-10-06
3
305 Views
Last Modified: 2006-11-17
How to read a short text file (like "Hello World") in byte array?
0
Comment
Question by:edi02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

738 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