Replicate "Save as HTML complete" in Java Action - java

How can I replicate the "Save as HTML, complete" command in a Java action?
I need to save a webpage, but it has local references such as "css/screen.css" or "images/logo.png".
If I manually save it with Firefox, for instance, I get a contents folders so that it all works.
Didn't find a way to do the same in Java, all I have is a code to download just the HTML source, but it still has local references.
Any tip?
URL url = new URL("http://www.google.it");
BufferedReader reader = new BufferedReader
(new InputStreamReader(url.openStream()));
BufferedWriter writer = new BufferedWriter
(new FileWriter("data.html"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
I know I may parse the whole html source in another moment and replace all references, but doesn't sound as a good choice. So, is there a way to download the WHOLE content?

Related

Storing values to be retrieved by user

I'm working on a program that allows the user to input and view passwords that are encrypted when inputted and decrypted when accessed by the user. I figured that this could be done using a text document, however I'm unsure of how to get my code to interact with said document. Any ideas on how to do this/alternative ways that I can accomplish my task?
You could save your input in a text file with BufferedWriter and read the document with BufferedReader.
An example would be:
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(password)
out.close
BufferedReader in = new BufferedReader(new FileReader(file));
while((line = in.readLine()) != null){
System.out.println(line);
}
in.close

I can't read a text file in GWT

I am developing a project with GWT and Netbeans. I have an RPC. I have put a text file in the server package "org.myname.server" and I want to read it with a server side method belonging to the class GWTServiceImpl. The text file and the file GWTServiceImpl.java are in the same package. The code is the following:
String text="";
try
{
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String line;
while((line = br.readLine()) != null)
{
text=text+line;
System.out.println("here is the line: "+line);
}
br.close();
}
catch (Exception e) { }
return text;
It says that it can't access the file. I haven't included the entire path because the file is in the same folder of the method. So why doesn't it work?
File paths aren't relative to “classes”, but to the “current working directory”, so it'll depend how your server is launched, and will likely be different in development and production.
If the file is packaged as a resource in your webapp, then use the appropriate way of loading it: if it's in WEB-INF/classes or in a JAR in WEB-INF/lib, then use getClass().getResourceAsStream("file.txt"); otherwise use ServletRequest#getResourceAsStream().
Yes Thomas is right. So in order to create the buffered reader the code is the following:
InputStream is= getClass().getResourceAsStream(filepath);
BufferedReader br = new BufferedReader(new InputStreamReader(is));

Reading text from file in GUI

I am stuck at reading text from a file into text area.I don't know why but my file reader never opens the file even if it exists.I am getting file name from a text field and using a button listener to trigger this event.So any help will be appreciated. I've given my code to below.
try{
BufferedReader br = new BufferedReader(new FileReader(tf1.getText()));
while((read = br.readLine())!=null){
store = store + read;
}
ta.setText(store);
fr.close();
br.close();
jf2.dispose();
}
catch(Exception exp){
JOptionPane.showMessageDialog(null,"File Not Found.");
}
Change your code to something like this:
br = new BufferedReader(new FileReader(new File(tf1.getText())));
It is important to note that you need to have a "File" that encapsulates your text to open the actual file. Otherwise, the JVM does not know in which part of the harddisk to search for.
Good luck.

file delete and rename not working

Here is the code but it does not delete storedIp file and rename tempFile to storedIP. Both file exist
String host=ipParsing(hostName);
File tempFile= new File("tempFile.txt");
File strFile = new File("StoredIp.txt");
BufferedReader bufferReader = new BufferedReader( new FileReader(strFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
String line;
while ((line = bufferReader.readLine()) != null) {
if(host.equals(line))
{
found=true;
line="";
}
bw.write(line);
if(!line.equals(""))
bw.newLine();
}
bw.close();
bufferReader.close();
strFile.delete();
tempFile.renameTo(new File ("StoredIP.txt"));
Well, a call to File.delete() does not necessary delete the file.
As the JavaDoc says: be sure to check the return value.
Ignoring this (like you did) is a common source of errors.
One occasion where this delete/renameTo easily goes awry, is when the files are in use. A solution seen consists of using an additional lock file. Too complicated for such a simple thing.
Using an embedded database, like java's own Derby, which is not that difficult. The database needs no extra provision. There are good tutorials with simple example code.

why does this reader read off strange bits of data?

I'm trying to read a text file, i'm using fileImputStream, and reading all the lines into a single String then outputing it into the console (System.out)
When I try to read the humanSerf.txt, it gives me this in the consol:
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural
\f0\fs24 \cf0 symbol=HS\
strength=15\
agility=13\
constitution=7\
wisdom=9\
intelligence=5}
in the text file, it says this:
symbol=HS
strength=15
agility=13
constitution=7
wisdom=9
intelligence=5
How do I make the weird text disappear?
this is the code i'm using, please help
try{
// Open the file that is the first
// command line parameter
FileInputStream read = new FileInputStream("resources/monsters/human/humanSerf.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(read);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
How do I make the weird text disappear?
ps, this was done in mac textedditor
I think your text file is not plain text but rather a RTF file which supports formatting. When you view it you probably use a tool which supports RTF, such as TextEdit. If you view it with less or cat you should also see the RTF markup.

Categories