i am not getting the desired output(the contents in a text file which i have saved in the jdk bin here is my code i got. Also what is return in the try catch block i wanna know.

it says file not found

import java.io.*;

class FileDemo
{
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
try
{
fin=new FileInputStream("nikhil.txt");
}
catch(FileNotFoundException e)
{
System.out.println("file not found");
return;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("no filename specified");
return;
}
do
{
i=fin.read();
if(i!=1)
System.out.print((char)i);
}
while(i!=-1);
fin.close();
}
}

i have created a text file nikhil stored in

C:\Program Files\Java\jdk1.6.0_02\bin

i am not getting the desired output(the contents in a text file which i have saved in the jdk bin here is my code i got. Also what is return in the try catch block i wanna know.

it says file not found

import java.io.*;

class FileDemo
{
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
try
{
fin=new FileInputStream("nikhil.txt");
}
catch(FileNotFoundException e)
{
System.out.println("file not found");
return;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("no filename specified");
return;
}
do
{
i=fin.read();
if(i!=1)
System.out.print((char)i);
}
while(i!=-1);
fin.close();
}
}

i have created a text file nikhil stored in

C:\Program Files\Java\jdk1.6.0_02\bin

Okay, nikhil.txt is in C:\Program Files\Java\jdk1.6.0_02\bin, but where is FileDemo.class located and, if you typed in "java FileDemo" to run this, what directory were you in when you did so? Normally you do not put your text files in C:\Program Files\Java\jdk1.6.0_02\bin, nor do you put your .class files in there. Put FileDemo.java and nikhil.txt in the same directory, go to that directory, compile FileDemo.java to get FileDemo.class, then type "java FileDemo" while in that same directory and see if this time, the file is found. If you've set your Path correctly and put C:\Program Files\Java\jdk1.6.0_02\bin in that Path, you DO NOT need to be in C:\Program Files\Java\jdk1.6.0_02\bin when executing java.exe or javac.exe.

i tried that too.but same problem.if i use the outputstream procedure and write the file myself i get the output while reading the file.

import java.io.*;

class FileOutputDemo 
{	

        public static void main(String args[])
        {              
                FileOutputStream out; // declare a file output object
                PrintStream p; // declare a print stream object

                try
                {
                        // Create a new file output stream
                        // connected to "myfile.txt"
                        out = new FileOutputStream("reverse1.txt");

                        // Connect print stream to the output stream
                        p = new PrintStream( out );
		
                        p.println ("hi how r u");

                        p.close();
                }
                catch (Exception e)
                {
                        System.err.println ("Error writing to file");
                }
        }
}

if i try to read reverse1.txt using fileinputstream object only then i get " hi how are you"

ok i dont get the file not found error now with the following code but it does not display the contents of the file in the java screen

import java.io.*;
class CopyFile1
{
public static void main(String args[])throws IOException
{
int i;
FileInputStream fin;
try
{
try
{
fin=new FileInputStream("reverse1.txt");
}
catch(FileNotFoundException e)
{
System.out.println("source file not found");
return;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("error incorrect usage");
return;
}
try
{
do
{
i=fin.read();
}
while(i!=-1);
}
catch(IOException e)
{
System.out.println("file error");
}
fin.close();

}
}

Well you don't get "File Not Found" anymore, right? What's the file contents and what do you get when you run the input file? Seems like it's working???

[edit]
We posted at the same time. What are the file contents and what is the program output?
[/edit]

the output i expect on the java screen is "hi how r u?" which is what the text file contains("reverse1.txt")

but when i run java CopyFile1 it just gives no output and displays the next blank dos prompt. i have compiled the file in the same directory as the text file. i tred various methods referring various books but everywhere there is no output and sometimes file not found error. The contents of the file are not displayed.

help

regards

i got this from a book

import java.io.*;
class IO
{
public static void main(String args[])throws IOException
{
FileReader file=new FileReader("reverse1.txt");
BufferedReader br=new BufferedReader(file);
String text;
for(int counter=0;counter<20;counter++)
{
text=br.readLine();
System.out.println(text);
}
br.close();
}
}

this works .is there anything wrong with my fileinputstream class?

Maybe I missed it, but there's no code to display the contents, just to read it and display any errors.

edit: parallel post with previous - I was referring to code in post 4.

yes right silly me..thanks anyways hope to get going with you guys for a long time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.