force
July 15, 2006, 10:21pm
1
How would you link to a file that is hosted on tomcat’s server?
The filepath would be C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\PCM\myfile.text
or
http://localhost:8080/PCM/myfile.txt
String theFileName="myfile.txt";
BufferedReader inputStream = new BufferedReader(new FileReader(theFileName));
Hi,
FileReader will pull from the operating system file path, so the first one.
rozner
July 17, 2006, 3:25pm
3
Here’s a more generic solution (will work as long as this is a servlet)
String theFile = this.getServletContext().getRealPath("/myfile.txt");
// ... rest of your code
If you do something like “localhost:8080”, what happens when if you change the server to run on port 8081, or the bigger problem, what if you’re not accessing the page from that system? it will need to be <your_IP>:8080, or domain name.
force
July 18, 2006, 7:39pm
4
ok, you say that it will work if it’s a servlet…what about a JSP?
[edit]: I couldn’t get it working in the servlet. It didn’t seem to like the “this” reference. What’s the trick?
if ‘this’ is a problem, you’re probably using a static method, which is only good if you want to have serious problems with threads stepping on each other…in other words: lose the static modifier off the method declaration.
force
July 18, 2006, 9:18pm
6
It was. New problem: getServletContext() is undefined.
Alright, here’s the full source code. I’m getting nowhere with this. How exactly am I supposed to read/write a file with a relative file path?
import java.io.*;
import java.net.*;
import java.util.*;
public class BuildAds {
public List ReadAdList(String theFileName){
ArrayList adlist = new ArrayList();
try {
String thePath = this.getServletContext().getRealPath(theFileName);
BufferedReader inputStream = new BufferedReader(new FileReader(thePath));
//BufferedReader inputStream = new BufferedReader(new FileReader(theFileName));
//URL url = new URL(theFileName);
//BufferedReader inputStream = new BufferedReader(new InputStreamReader(url.openStream()));
String line=null;
line=inputStream.readLine();
while(line!=null){
String adtitle = line;
String adcode = inputStream.readLine();
adlist.add(new BeanAds(adtitle,adcode));
System.out.println(adtitle + "\\r\
" + adcode);
line = inputStream.readLine();
}
inputStream.close();
} catch (FileNotFoundException e) {
System.out.println("File not found or could not be opened.");
e.printStackTrace();
}
catch (IOException e){
System.out.println("Error reading from file.");
e.printStackTrace();
}
return adlist;
}
public static void WriteAdList(String title, String code, String theFileName){
PrintWriter outputStream = null;
try {
outputStream = new PrintWriter(new FileOutputStream(theFileName, true));
URL url = new URL(theFileName);
outputStream = new PrintWriter(new FileOutputStream(url.getFile(), true));
//outputStream = new PrintWriter(new FileOutputStream(theFileName, true));
//String theFile = this.getServletContext().getRealPath("/myfile.txt");
outputStream.println(title);
outputStream.println(code);
outputStream.close();
} catch (FileNotFoundException e) {
System.out.println("File not found or could not be opened.");
e.printStackTrace();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
public class BeanAds {
private String adtitle, adcode;
public BeanAds(String adtitle, String adcode){
this.adtitle=adtitle;
this.adcode=adcode;
}
public String getAdcode() {
return adcode;
}
public void setAdcode(String adcode) {
this.adcode = adcode;
}
public String getAdtitle() {
return adtitle;
}
public void setAdtitle(String adtitle) {
this.adtitle = adtitle;
}
}
Is this running as a Servlet?
I ask because BuildAds doesn’t extend HttpServlet, and you’re not using doPost or doGet.
(getServletContext() is a GenericServlet method, accessable, of course, from a class that extends HttpServlet, because HttpServlet extends GenericServlet)
Is this a helper class (called from a Servlet)? If so, you’ll need to get the path with the Servlet, then pass it as a parameter.
force
July 18, 2006, 9:51pm
8
Yes, it’s a helper class.
It gets called from two different places. One is from a servlet, like you said, and the other is from a JSP.
[edit]: The file write method only gets called from a servlet.
[edit2]: Ok, your method of passing the path from the servlet worked for both the servlet and the JSP. Will the same thing work for the write method?
[edit3]: heh, looks like I answered my own question. It works the same.
Thanks for the help guys!
rushiku, thanks for pointing me in the right direction yet again
I don’t see why not, try it and let us know.
force
July 18, 2006, 10:28pm
10
Here’s the working code:
BuildAds.WriteAdList(title_ad, code_ad, this.getServletContext().getRealPath("myfile.txt"));
List ad2_list = BuildAds.ReadAdList(this.getServletContext().getRealPath("myfile.txt"));
import java.io.*;
import java.net.*;
import java.util.*;
public class BuildAds {
public static List ReadAdList(String theFilePath){
ArrayList adlist = new ArrayList();
try {
BufferedReader inputStream = new BufferedReader(new FileReader(theFilePath));
//BufferedReader inputStream = new BufferedReader(new FileReader(theFileName));
//URL url = new URL(theFileName);
//BufferedReader inputStream = new BufferedReader(new InputStreamReader(url.openStream()));
String line=null;
line=inputStream.readLine();
while(line!=null){
String adtitle = line;
String adcode = inputStream.readLine();
adlist.add(new BeanAds(adtitle,adcode));
//System.out.println(adtitle + "\\r\
" + adcode);
line = inputStream.readLine();
}
inputStream.close();
} catch (FileNotFoundException e) {
System.out.println("File not found or could not be opened.");
e.printStackTrace();
}
catch (IOException e){
System.out.println("Error reading from file.");
e.printStackTrace();
}
return adlist;
}
public static void WriteAdList(String title, String code, String theFilePath){
PrintWriter outputStream = null;
try {
outputStream = new PrintWriter(new FileOutputStream(theFilePath, true));
//URL url = new URL(theFileName);
//outputStream = new PrintWriter(new FileOutputStream(url.getFile(), true));
outputStream.println("\\r\
[" + title + "]\\r\
");
outputStream.println(code);
outputStream.close();
} catch (FileNotFoundException e) {
System.out.println("File not found or could not be opened.");
e.printStackTrace();
}
//catch (MalformedURLException e) {
// e.printStackTrace();
//}
}
}