Displaying a text file on a webpage - java

I'm new to coding and have decided to start my learning on Java. I've got NetBeans and have started to create a very basic web application. I'd like to be able to display values from a .txt file onto the webpage, and I've got this code to do so.
<%
BufferedReader in = new BufferedReader(new FileReader("Cats.txt"));
String line;
while((line = in.readLine()) != null)
{
out.println(line);
}
in.close();
%>
My text file is in the same folder as my src folder (As I've seen you need to put the file)
However, whenever I navigate to the web page I get a FileNotFound error. I've tried placing the files path in the FileReader but that gives an error due to the backslashes.
If anyone could help I'd be greatly appreciated

Currently it's looking for the file in the src directory of your application you should just be able move the file there and it should read it. If you would like to direct to a specific path you need to tell the IDE to treat the '\' as a normal slash to do this you need to close it off by using two '\'s instead of one eg:
<%
BufferedReader in = new BufferedReader(new
FileReader("C:\\MYPATH\\MYPATH2\\Cats.txt"));
String line;
while((line = in.readLine()) != null)
{
out.println(line);
}
in.close()
%>

Related

JAR file doesn't recognize update of a file that I'm reading in JAR

I'm using Netbeans and in my program I'm reading a file. When I run the program it reads the file correctly. When I build the program the JAR also works correctly. But when I change the file that I am reading from, in my build dir, my JAR doesn't update accordingly. Why is that so? Is there a solution for this?
The code below shows how am I reading the file in my program. Thanks in advance.
InputStream in = NewJFrame.class.getResourceAsStream("/holidays.txt");
BufferedReader readHolidays = new BufferedReader(new InputStreamReader(in));
String line;
line = readHolidays.readLine();
while(line != null) {
//read into hashmaps
//...
line = readHolidays.readLine();
}
readHolidays.close();
Never mind, i got it ;) If anyone else needs this: Basically every time i run the program i find the file path to the jar file and from there i read text document.
File jarFile = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
FileInputStream is = new FileInputStream(jarFile.getParent().toString() + "/file.txt"); BufferedReader readFile= new BufferedReader(new InputStreamReader(is));

Unable to read the file in Eclipse

I have a project in our university and in order to make it we have to read a txt file(in java,eclispe). I take the file and drag and drop (the method which reads the file they gave it ready to us) but when I run the program it gives me this:
I do not know if the mistake is how I put the file in the Eclipse
The folder name above src is non-English so I can't type it out, but suppose it were just Laur155. Suppose that this folder is directly under ~/workspace. Then to open the file you would do
String filename = "~/workspace/Laur155/NBA-5d-17265n.txt";
then something like
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) { ...
No we must not change the code in the method which reads the txt file
I know maybe i confuse you but in my university they havent teach as nothing about files

Get automatically a CSV from external server using Spring MVC

I have a scheduled task (using cron) inside my Spring MVC application. Inside the programmed task I have to get a CSV from an external server in the following link:
http://www.aemet.es/es/eltiempo/observacion/ultimosdatos_6172O_datos-horarios.csv?k=and&l=6172O&datos=det&w=0&f=temperatura&x=h24
And once I get it I have to parse it.
The problem comes when getting the file, as when I click on the previous link I can download it to my computer, but I don't know how to do that using Spring.... can you give me I hint??
UPDATE: I don't have any code yet, but I guess that must be something similar to the following code:
URL stockURL = new URL("http://example.com/stock.csv");
BufferedReader in = new BufferedReader(new InputStreamReader(stockUrl.openStream()));
CSVReader reader = new CSVReader(in);
But the problem is that my URL is not exactly a .csv. Whe I put the URL in a browser it looks like it is a redirect.
Thank you very much indeed.
Thank you all for your comments. Even if the URL doesn't have a CSV extension, i tried the following code (in Java, not Spring) but it works!!
URL stockURL = new URL("http://www.aemet.es/es/eltiempo/observacion/ultimosdatos_6172O_datos-horarios.csv?k=and&l=6172O&datos=det&w=0&f=temperatura&x=h24");
BufferedReader in = new BufferedReader(new InputStreamReader(stockURL.openStream()));
//CSVReader reader = new CSVReader(in);
String line;
while((line = in.readLine()) != null){
System.out.println(line);
}
So,I guess that using Spring is going to be really very similar to get the file, so thank you very much everybody!

Making web crawler download multiple web pages locally

I would like my web crawler to download all the browsed URL's locally. At the minute it will download every site it comes to but then overwrite the local file in each website visited. The crawler start at www.bbc.co.uk, downloads that file and then when it hits another it overwrites that file with the next URL. How can I make it download them in to single files so I have a collection at the end? I have this code below but I dont know where to go from here. Any advice would be great. The URL inside the brackets (URL) is a string which is used to manipulate all the browsed webpages.
URL url = new URL(URL);
BufferedWriter writer;
try (BufferedReader reader = new BufferedReader
(new InputStreamReader(url.openStream()))) {
writer = new BufferedWriter
(new FileWriter("c:/temp/data.html", true));
String line;
while ((line = reader.readLine()) != null) {
//System.out.println(line);
writer.write(line);
writer.newLine();
}
}
writer.close();
You need to give to your files a unique name.
You can save them in different folders (one root directory for each web site).
Or you can give them a unique name (using a counter for example).

java file relative path in eclipse

Three days i was trying to figure out how to read file using relative file path. In eclipse this compiles and works great, but when i export app. It says that it can't find the file. here is the screenshot and code i work on.
This code works, but only in eclipse, it compiles and does job perfectly. But when i export it as as runnable jar file i get an error, that it cannot locate licenca.txt
BufferedReader in = new BufferedReader(new FileReader(new File("licenca.txt").getPath()));
String str;
while ((str = in.readLine()) != null) {
taLicenca.append(str + "\n");
}
here is the screenshot of my project files
i have tried use of scanner function, still the same result, it works in eclipse, but doesn't work on export. Here is the error message:
I'll bet it'll work if you put that file into the classpath.
Change your code like this:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("licenca.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String str;
while ((str = in.readLine()) != null) {
taLicenca.append(str + "\n");
}
Try it and see.
It happens because your file is exported as part of jar file, so, for creating jar file try to use ant or maven or semething else, or manually copy your file in directory with with your jar, it calls start directory.

Categories

Resources