Scanning and displaying every word from a website source code Java - java

I have been given a task to scan the contents of a website's source code, and use delimiters to extract all hyperlinks from the site and display them. After some looking around online this is what I have so far:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class HyperlinkMain {
public static void main(String[] args) {
try {
Scanner in = new Scanner (System.in);
String URL = in.next();
URL website = new URL(URL);
BufferedReader input = new BufferedReader(new InputStreamReader(website.openStream()));
String inputLine;
while ((inputLine = input.readLine()) != null) {
// Process each line.
System.out.println(inputLine);
}
in.close();
} catch (MalformedURLException me) {
System.out.println(me);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
So my program can extract each line from the source code of a website and display it, but realistically I want it to extract each WORD as such from the source code rather than every line. I don't really know how it's done because I keep getting errors when I use input.read();

There is lots of source code around to retrieve web pages. Look at the Pattern class to see how to regex text for hyperlinks. You can treat your homework assignment as two separate problems by working on the hyperlink extraction separately from the web page downloads.

Related

How to write String from variable to text file

My goal here is to read lines from a text file, check if they are palindromes, and then write those to a completely different file.
Now the problem, as far as I can see, lies in the if statement block where I check for palindromes successfully but can't seem to write them to another file because they are stored in a variable.
When I use the BufferedWriter write method and set the parameters as an actual string with quotes, everything works.
How can I solve this?
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
public class Zadatak14 {
public static void main(String[] args) {
BufferedReader br;
try {
br = new BufferedReader(new FileReader("C:\\Users\\Luka\\Documents\\NetBeansProjects\\CS101-DZ14-14\\src\\zadatak14\\ulaz.txt"));
String line;
while ((line = br.readLine()) != null) {
String palindrome = new StringBuilder(line).reverse().toString();
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Luka\\Documents\\NetBeansProjects\\CS101-DZ14-14\\izlaz.txt"));
if (line.contains(palindrome)) {
System.out.println(line);
bw.write(line);
}
bw.close();
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("Greska");
} catch (IOException ex) {
System.out.println("Greska");
}
}
}
BufferedWriter bw = new BufferedWriter
You're making a new writer for every line in your input, which surely you don't want. Move the creation of the writer up top, right after opening the reader.
br.close();
not how you do that. This is how you do that:
try (BufferedWriter bw = new BufferedWriter(new FileWriter("...."))) {
// code here
}
// no matter how you get out of this code, the file is closed.
The reason you do it with the above is because that bw.close() won't get called if exceptions occur, if you return out of the block. With the try construct, that stream is closed when code exits from the block no matter how it does that.
} catch (FileNotFoundException e) {
System.out.println("Greska");
Not how you do that - exceptions contain a lot of useful info (at least 4 things, more for some specific kinds of exceptions: type, message, stack trace, cause), and you're tossing it all in the garbage. It's a bad idea to toss information about problems in the bin. Don't. The right move is to add throws Exception to your public static void main(String[] args) method, and then you don't need a catch at all. If you must, the correct body for a catch block if you don't have a good way to handle the problem, is throw new RuntimeException("Uncaught", e);. This preserves all information and doesn't cause errors about having to catch exceptions.

Cannot resolve method 'openFile(java.lang.String)'

import java.io.File;
import java.util.Scanner
import java.io.FileNotFoundException;
public class hello {
public static void main (String [] args){
File file = new File("example.txt");
try {
openFile("example.txt");
} catch (FileNotFoundException exception){
System.out.println("Cannot find that file");
}
}
}
java : cannot find symbol
symbol : method openFile(java.lang.String)
Cannot resolve method 'openFile(java.lang.String)'
EDÄ°TED (thanks for our helps)
import java.io.File;
import java.io.FileNotFoundException;
public class hello {
public static void main(String [] args) {
try{
File file = new File("example.txt");
Scanner scanner = new Scanner(file);
System.out.println("opened");
}
catch (FileNotFoundException exception){
System.out.println("Cannot find that file");
}
}
}
Ok, I'm going to assume a few things here. Given that its a text file and you're trying to open it, I'm guessing you're trying to read it,if not then edit your question.
You need a bufferedReader, you can probably find an implementation for your specific case but the basics are:
create a buffered reader (called "reader" for example)
Use the following line:
reader = new BufferedReader(new FileReader(file));
Where file is the path to the .txt file.
Then to read do use
reader.readLine());
Put a while loop around this where the condition is that reader.readLine is not null
and also put a try/catch around it.
Hope this helps, I'm making a few assumptions but reading a .txt file is quite common and it looks like that's what you're trying to do.

java exec run python program, while python has no authority to I/O

The following code has I/O authority in my local PC and runs correctly. However when I've tried to do this on my windows server2012, some problems occured. It can't run python code correctly by using exec, my python cannot give the final result. I think it has no authority to I/O, but why?
Java code:
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class test {
private static void generateFile() throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter("src/test/input.txt", false));
String eventString = "This is a example";
bw.write(eventString);
bw.close();
}
private static void getFile() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("src/test/output.txt"));
String jsonResults = br.readLine();
br.close();
}
public static void main(String[] args) throws IOException {
generateFile();
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("python test.py", null, new File("src/test/"));
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
try {
pr.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
pr.destroy();
}
getFile();
}
}
python code:
f = open('input.txt', 'r')
line = f.readline()
with open('output.txt', 'w') as fw:
fw.write(line)
print("Done!")
ERROR infomation :
Exception in thread "main" java.io.FileNotFoundException: src\test\output.txt (The system cannot find the specified file.)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at test.test.getFile(test.java:21)
at test.test.main(test.java:49)
Various things here (for the moment more "code quality" hints):
Your poor method is doing way too many things. Example:
Individual "parts", like the following - should all go into a simple helper methods:
String eventString = getJsonEventListInTopic(topicLabel);
FileWriter fw = ...
fw.close();
Meaning: you want to read about the Single Layer Of Abstraction principle
The core point to make progress on your actual question: when you run your python script manually on that windows system (with exactly the same arguments/parameters) on the command line - does that work? You know, it could be something super-simple as "python.exe is not in your path"

Deleting a line that starts with a particular number in a text file

I've been trying to come up with a class that deletes a line from a text file that starts with a particular number.
What I currently have doesn't show any code errors and also runs without erros; shows "BUILD SUCCESSFUL" on netbeans, but doesn't do anything to the line, or any part of the textfile whatsoever, let alone delete the intended line.
Could anyone please look at my code and please advise me on what I might have done wrong, or is missing?
Thanks a lot in advance.
Heres my code:
package Database;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Edit {
public void removeLineFromFile(String file, String lineToRemove) {
try {
File inFile = new File("/D:/TestFile.txt/");
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (!line.trim().equals(line.startsWith(lineToRemove))) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
Edit edit = new Edit();
edit.removeLineFromFile("/D:/TestFile.txt/", "2013001");
}
}
There is a problem with your logic ... you are saying if the line equals to itself that starts with something which will never happen unless the line only consist of the line you want to remove
if (!line.trim().equals(line.startsWith(lineToRemove))
i think needs to be just
if (!line.startsWith(lineToRemove))
Change the if condition to:
if (!line.startsWith(lineToRemove)) {
pw.println(line);
pw.flush();
}

Android app to push updating data

I new to developing in android but I'm not in java. So I want to develop an app that checks for football match scores and as soon as new data is available the android app must push that data to the user in a notification fashion.
My question is the following:
Can I use a website's server that is not mine to get data from since I dont have a server to use. Therefore I cant use C2DM
If not what is the solution to this: TCP/IP connection or can I customize a webview to my liking?
Thanks in advance,
Roy
My experience with using internet data, however this may help you start.
This is the class that I use to download webpages and return them as a string, it should be possible to parse the page data to extract the data that you want. Something you should be careful is that it can be common for webpages to change their format which could break your parsing functionality, perhaps without you even realising.
Check this out for Java HTML parsers
package AppZappy.NIRailAndBus;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Simplifies downloading of files from the internet
*/
public class FileDownloading
{
/**
* Download a text file from the Internet
* #param targetUrl The URL of the file to download
* #return The string contents of the files OR NULL if error occurred
*/
public static String downloadFile(String targetUrl)
{
BufferedReader in = null;
try
{
// Create a URL for the desired page
URL url = new URL(targetUrl);
// Read all the text returned by the server
in = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder sb = new StringBuilder(16384); // 16kb
String str = in.readLine();
if (str != null)
{
sb.append(str);
str = in.readLine();
}
while (str != null)
{
// str is one line of text; readLine() strips the newline
// character(s)
sb.append(C.new_line());
sb.append(str);
str = in.readLine();
}
String output = sb.toString();
return output;
}
catch (MalformedURLException e)
{}
catch (IOException e)
{}
finally
{
try
{
if (in != null) in.close();
}
catch (IOException e)
{
}
}
return null;
}
private FileDownloading()
{}
}

Categories