Solved

How to read a text file and output on screen ?

Posted on 2001-07-03
5
177 Views
Last Modified: 2008-02-01
Below code is taken from EE's answer. It would read a text file "ReadFile.java", and output it on the screen.

When i put it as <applet>, or i run it using "java ReadFile.class", there is no output on my screen.

Is it because of any io or security issue ? how to read a text file and output it on the screen ?

Thanks
Collin
-----------------------------------------

import java.io.*;
public class ReadFile
{
    public static void main(String args[])
         throws Exception
    {
         FileInputStream fin=new FileInputStream("ReadFile.java");
         BufferedInputStream bin=new BufferedInputStream(fin);
         String result;
         int MAX_SIZE=1024;
         byte b[]=new byte[MAX_SIZE];
         int i,count=0;
         while ( (i=bin.read()) != -1  )
         {
              b[count]=(byte)i;
              count++;
              if (count >= MAX_SIZE)
              {
                   
              }
         }                  
         result=new String(b);
         System.out.println();
    }
}
0
Comment
Question by:collinng
5 Comments
 
LVL 2

Accepted Solution

by:
mzimmer74 earned 50 total points
ID: 6250142
Well, you never actually print anything to the screen.  If you want to see what you read in change the System.out.println(); statement to System.out.println(result);
0
 
LVL 6

Expert Comment

by:dorothy2
ID: 6250158
The only thing this program is printing out to the screen is a new line character, which comes from the final "System.out.println();", which is outside your while loop. Try substituting "System.out.println(result);".
Also is the file you are trying to read in the same directory as your class file? I'm not sure whether the FileInputStream generates a File Not Found exception if it can't find the file. You could pass it a File object rather than a String, and then you would get that exception.

Dorothy
0
 

Author Comment

by:collinng
ID: 6251105
i added the var "result", ie. System.out.println(result);

(a)
when i use an applet to run JavaRead.Class, there is no output. should i use setText and textbox ?

(b)
When i just type "java ReadFile.class" the err msg is:

Exception in thread "main" java.lang.NoClassDefFoundError: ReadFile/class.


Is there a certificate issue ? i hope not.
Thanks for any comments.

Collin

0
 
LVL 3

Expert Comment

by:rjackman
ID: 6251720
hi
 pls tell me the meaning of following
>>when i use an applet to run JavaRead.Class

r u using appletviewer
or  r u trying to run the applet from browser?
another thing is u should not suppy the extension while running the classfile
ie instead of java JavaRead.class
pls type
java JavaRead
cheers
RJ
0
 

Author Comment

by:collinng
ID: 6252895

To clarify:

"When i use applet to run ReadFile.class"

<applet code="ReadFile.class" width=300 height=300>
</applet>

i add the above as in a html file, and open the html file using a browser.
 
Comment:

When i type "Java ReadFile" it would output on the dos screen. Is there a tip to see the output on the browser ?

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 57
split string containing \r\n in Java 46 44
Arrays.asList  VS  ArrayList 4 59
tomcat startup error 5 65
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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.

777 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