Reading a txt file from a web application
Instead of having the file on my C drive I would like to have it in the web application.. Where should I put it & how should I call it ? I am not using a servlet or jsp but this code below is in a class file which will be used by a servlet/jsp
publicstaticint myMethod(){
int countRec=0;
try
{
RandomAccessFile randFile =new RandomAccessFile("c:\\myfile.txt","r");
long lastRec=randFile.length();
randFile.close();
FileReader fileRead =new FileReader("c:\\myfile.txt");
LineNumberReader lineRead =new LineNumberReader(fileRead);
lineRead.skip(lastRec);
countRec=lineRead.getLineNumber()+1;
fileRead.close();
lineRead.close();
}
catch(IOException e){...}
return countRec;
}
Is it on the server, or are you trying to upload it from the client?If it's on the server, what you have looks like it should work (though I only glanced at it). One thing though--put the close() in a finally block.What specific problems you are having?
I wanted to wrap the txt file somewhere in the war file and reference it from this class file so I dont have to worry about the location of the file
Oh, okay.Then put it somewhere that will be on the classpath, and use Class.getResource or getResourceAsStream.
You are certainly pulling a stunt with that RandomAccessFile code.I don't think you'll be able to repeat it with the URL/InputStreamreturned by getResource/getResourceAsStream.
> You are certainly pulling a stunt with that> RandomAccessFile code.> I don't think you'll be able to repeat it with the> URL/InputStream> returned by getResource/getResourceAsStream.Good point. I wasn't paying enough attention.
> Oh, okay.
>
> Then put it somewhere that will be on the classpath,
> and use Class.getResource or getResourceAsStream.
The answer is probably really simple but Since Class.getResource returns a URL .. how would I use the RandomAccessFile which doesnt take URL
public static int myMethod(){
int countRec=0;
try
{
URL url = MyClass.class.getResource("myfile.txt");
RandomAccessFile randFile = new RandomAccessFile("c:\\myfile.txt","r");
long lastRec=randFile.length();
randFile.close();
FileReader fileRead = new FileReader("c:\\myfile.txt");
LineNumberReader lineRead = new LineNumberReader(fileRead);
lineRead.skip(lastRec);
countRec=lineRead.getLineNumber()+1;
fileRead.close();
lineRead.close();
}
catch(IOException e){...}
return countRec;
}
> how would I use the RandomAccessFile which doesnt take URLGive up. Just read through the file to find the #lines.
Thank you jverd!
I got it ..
URL url = MyClass.class.getResource("/questions.txt");
File file = new File(url.getPath());
RandomAccessFile randFile = new RandomAccessFile(file,"r");
long lastRec=randFile.length();
randFile.close();
FileReader fileRead = new FileReader(file);
LineNumberReader lineRead = new LineNumberReader(fileRead);
lineRead.skip(lastRec);
countRec=lineRead.getLineNumber()+1;
fileRead.close();
lineRead.close();
> File file = new File(url.getPath());What if your web app is in a war file?
That will only work if the file in question is on the file system. Not if it's inside a jar, not if it comes over the network, etc.
spoke too soon.. that didnt work...> > File file = new File(url.getPath());> > What if your web app is in a war file?Well that was the first question in this post?
Can you try doing it without the RandomAccessFile, just to make an old man happy?
I am using the RandomAccessFile later in the code..
I am trying to generate random questions from a file.. My random number generator generates numbers. Those numbers are used to pick questions from the file.
example: If the random number selected is 7 then questions[6] which contains the question picked.
for (int i = 0; i<getNumberOfQuestions(); i ++){
questions[i] = raf.readLine();
}
Not too familiar with the io package but the RAF is working for me>
Is the file too big to just read into memory once and for all as a list of questions?And if it's too bug to do that, why not use a database?
So just read the file in once and put the questions into an array, or a List or something. Then choose a random element from the array or List. Reading the file over and over isn't a good choice -- unless you have hundreds of thousands of questions and start using up all your memory.
> And if it's too bug to do that, why not use a database?Freudian slip
The file is not too big.. I am not sure what you mean by read in memory once...wont the garbage collector remove it from memory?
In the webapp I am reading once per person (each person is asked questions randomly) when I run the program.. Do you mean I should use "singleton" to maintain questions in memory?
Also if I use List to randomly read the lines I dont want questions to repeat again so I will have to remove them from the list after picking it .. in which case I will read the file again.
How does reading in memory once help me with the Class.getResource? Can you show me in code what you mean?
> The file is not too big.. I am not sure what you mean
> by read in memory once...wont the garbage collector
> remove it from memory?
Only if you do not maintain a reference to it.
> In the webapp I am reading once per person (each
> person is asked questions randomly) when I run the
> program.. Do you mean I should use "singleton" to
> maintain questions in memory?
That's one way. I think Singletons are not recommended with
web apps, right? Something to do with replication or clustering
or whatever that buzzword is. Instead you add it to your application's
context, say using ServletContext's get/setAttribute.
> Also if I use List to randomly read the lines I dont
> want questions to repeat again so I will have to
> remove them from the list after picking it .. in
> which case I will read the file again.
No. A little insight into how to do random selection
without repetition will solve this problem.
> How does reading in memory once help me with the
> Class.getResource? Can you show me in code what you
> mean?
I think you've already written this code, but since I was
looking at ServletContext, I noticed this method in the class:
InputStream getResourceAsStream(String path)
It seems to be preferred over Class's version.
> The file is not too big.. I am not sure what you mean
> by read in memory once...wont the garbage collector
> remove it from memory?
Only if you are careless enough not to keep a link to it.
> In the webapp I am reading once per person (each
> person is asked questions randomly) when I run the
> program.. Do you mean I should use "singleton" to
> maintain questions in memory?
I mean you should have a list of questions in memory. There would presumably only be one list, so you could call that a "singleton" if you liked, but I wouldn't bother with fancy terminology like that. If it's a web app you could put it in the application context.
> Also if I use List to randomly read the lines I dont
> want questions to repeat again so I will have to
> remove them from the list after picking it .. in
> which case I will read the file again.
Okay. (I really hate designs where new requirements pop up at random. It's better to get all the requirements together first before starting the design.) So you copy the base list to a list where you are going to select from.
> How does reading in memory once help me with the
> Class.getResource? Can you show me in code what you
> mean?
It doesn't help you with that. (Not that I understand what you mean by the question anyway.) I don't have time to write code just now but here's pseudocode:Get an InputStream on the file (via getResourceAsStream)
Create an empty List
Read lines from that InputStream until end of file
For each line read:
Add it to the list