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? ?

Recommended Answers

All 6 Replies

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.

What code did you try?

Ok..I will write it ....

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.

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.