Results 1 to 3 of 3
Thread: method
- 11-17-2009, 06:26 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 12
- Rep Power
- 0
method
just wondering, do i have to putJava Code:import java.io.*; import java.util.Scanner; class ReturnFileLine { private String files; private String fileread; public ReturnFileLine(String filename) { files = filename; } public int total() throws IOException { File in = new File(files); Scanner read = new Scanner(in); int inc=0; do { inc = inc+1; fileread = read.nextLine(); } while (read.hasNextLine()); return inc; } public String readLine(int num) throws IOException { File in = new File(files); Scanner read = new Scanner(in); String readLine=null; for (int n=1;n<=num;n++) { readLine = read.nextLine(); } return readLine; } } class ReadFile { public static void main(String[] args) throws IOException { ReturnFileLine rfl = new ReturnFileLine("E:\\Web\\web.txt"); System.out.println("Total lines " +rfl.total()); System.out.println("Text at line 40 are:" +rfl.readLine(40)); } }
in method every time i want to read file?File in = new File(files);
Scanner read = new Scanner(in);
Thank you
- 11-17-2009, 09:33 PM #2
no you can make those static variables(global variables???). Declare them inside your class but outside your methods.
static String files = new String("some file location");
static File in = new File(files);
static Scanner read = new Scanner(in);
You might also be able to link it all together.
static Scanner read = (new File(new String("File name"));
I don't reccomend doing it this way though unless you don't need to use files or in outside of that line.Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 11-18-2009, 09:52 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Store the File rather than the String. That is create the File object inthe constructor from the supplied filename. Other than that, you need to reopen the file for each method since they do different things. Sharing an IO resource between them doesn't make sense to me.
There's absolutlely no need to make things static. In fact I would argue it's a mistake, since you have defined a perfectly good class there with perfectly good attributes.
Similar Threads
-
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 07:20 PM -
calling method from main method
By bob_bee in forum New To JavaReplies: 4Last Post: 10-02-2009, 05:30 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks