Hi all,
In main program I read file using bufferedreader, and (try, catch),
Now this section is repeated in my code , how can I call function to do that ? By passing filepath ..I try it but I don't get any line of file ...
How can I get lines of files using function when I call it? ?

you want us to comment on your code without seeing your code?

based on your description: extract it in a method, and call that method in a loop.

I get null ...how can i call try ..catch and return line of any file by passing filepath???

public class  fn {
public static String readfile(String filepa) throws IOException{

try {
            FileReader fr = new FileReader(filepa);
            BufferedReader br = new BufferedReader(fr);

            line = br.readLine();

            while (line !=null) {
            int    offset = line.indexOf("//");

             if (-1 != offset) {
             line = line.substring(0, offset);                                    
                }

           System.out.println(line);

            line = br.readLine(); 
        }

        }
        catch (IOException e) {
         e.printStackTrace();
        }
    return(line);   
    }

public static void main(String[]argv) throws ClassFormatException, IOException {
{String filepa= "C:/NetBeansProjects/filename.java";
line=readfile(filepa);
System.out.println(line);
}}      

I get null ....
this means literally nothing. where do you get null? what is the contents of the file? is there a stacktrace? ...

That code does not return a null, or anything else. It's not valid Java and it won't compile. (However it's easy to see why it would return null if it did compile - just ask what value of line allows the method to return.)
Please stop wasting our time and post the correct code.
Also fix your indentation before posting so people can see why your brackets don't match.

Edited 8 Months Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.