Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm making a launcher for my game using Java Swing. I need a way to add a tumblr/wordpress feed onto the launcher. An example would be the minecraft launcher (If you don't know what it looks like, go to this link).
I was also thinking RSS could be useful because I've seen that mentioned on feeds and stuff like this so if there's a simple way with that then that'd be helpful too.
Anyway, how would I do this?
EDIT: How would I use jsoup in Swing?
Here's an example I have used to parse data from a page
private static final String url = "website";
public void getLatestUpdate() throws IOException {
try {
URL addr = new URL(url);
URLConnection con = addr.openConnection();
ArrayList<String> data = new ArrayList<String>();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
Pattern p = Pattern.compile("<span itemprop=.*?</span>");
Pattern p2 = Pattern.compile(">.*?<");
Matcher m = p.matcher(inputLine);
Matcher m2;
while (m.find()) {
m2 = p2.matcher(m.group());
while (m2.find()) {
data.add(m2.group().replaceAll("<", "").replaceAll(">", "").replaceAll("&", "").replaceAll("#", "").replaceAll(";", "").replaceAll("3", "3"));
}
}
}
in.close();
addr = null;
con = null;
message("(" + data.get(3) + ")" + ", at " + data.get(4));
} catch (Exception e) {
System.out.println("Error getting data from website.");
e.printStackTrace();
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to be able to go through a folder containing files and display the files that have been specified. I currently have it hard coded... Cc
public void searchResult(String a) throws IOException {
FileReader inputFile;
a = "C:\\IO\\Project.txt";
try {
inputFile = new FileReader(a);
BufferedReader br = new BufferedReader(inputFile);
while ((str = br.readLine()) != null) {
searchResult.setText(str);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(SearchResults.class.getName()).log(Level.SEVERE, null, ex);
}
}
Please, I need something more dynamic.
i currently have it hard coded
Do you understand how passing parameters work?
public void searchResult(String a) throws IOException
{
a = "C:\\IO\\Project.txt";
try {
inputFile = new FileReader(a);
What is the point of hardcoding the value of "a". The point of using parameters is to pass the file name as a parameter to you method.
So the code should simply be:
public void searchResult(String a) throws IOException
{
try {
inputFile = new FileReader(a);
Also the following makes no sense:
while ((str = br.readLine()) != null) {
searchResult.setText(str);
Every time you read a new line of text you replace the previous line of text. You need to append(...) the text.
Or, the better solution is to just use the read(...) method of the JTextArea to load data from the file.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code :
package ggg;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
public class regex {
public static void main( String args[] ){
// String to be scanned to find the pattern.
String line = "This order was placed FRO-DDA-6666666 %10.25 %10.12 FRO-DDA-8888888 for QT3000! OK?";
String pattern = "\\d+\\.\\d{2}";
String pattern2 = "\\d{7}";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
Pattern t = Pattern.compile(pattern2);
// Now create matcher object.
Matcher m = r.matcher(line);
Matcher g = t.matcher(line);
try {
PrintWriter writer = new PrintWriter("C:\\Users\\John\\workspace\\ggg\\src\\ggg\\text.txt", "UTF-8");
for (int i = 1; m.find() && g.find(); i++) {
writer.println(g.group(0)+"->"+m.group(0));
}
writer.close();
} catch (IOException ex) {}
}
}
And result is:
6666666->10.25
8888888->10.12
I want to write a simple code to read text.txt file and if "8888888" exist in this file then print what is the front of "8888888->", What should I do ?
For example in our result 10.12 is front of 888888->
Add this to your code after building the file:
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\John\\workspace\\ggg\\src\\ggg\\text.txt"));
String myLine;
while ((myLine = br.readLine()) != null) {
if(myLine.contains("8888888"))
System.out.println(myLine.substring(myLine.indexOf(">")+1));
}
br.close();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to know if it's possible to do this:
i've some .txt file in a directory in my filesystem
i would like to write a java code that does this:
Automatically read all the files in the directory
Give me a output
Exists some library? or it's just a code problem?
It's possible?
Thanks
Reads & prints the content
public static void main(String[] args) {
List<String> li=new TestClass().textFiles("your Directory");
for(String s:li){
try(BufferedReader br = new BufferedReader(new FileReader(s))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
System.out.println(everything);
} catch (IOException e) {
e.printStackTrace();
}
}
}
For getting all Text files in the Directory
List<String> textFiles(String directory) {
List<String> textFiles = new ArrayList<String>();
File dir = new File(directory);
for (File file : dir.listFiles()) {
if (file.getName().endsWith((".txt"))) {
textFiles.add(file.getPath());
}
}
return textFiles;
}
Of course it's possible. You need to look at File, Reader classes. A useful method is File#listFiles. Happy coding.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have searched about this. All I got was xml parsing/ Sax parser. I need a program that will download xml data.
I need this for my android application development. thanks
For example i have a website localhost:8080/folder/sample.html.. How do i get a .xml file from that?
Sorry if I'm not answering the question - but is it the website content, you want to download? If positive, these are similar questions where the solution may lie:
How to get a web page's source code from Java
Get source of website in java
How do I retrieve a URL from a web site using Java?
How do you Programmatically Download a Webpage in Java
A good library to do URL Query String manipulation in Java
try this code:
public String getXmlText(String urlXml) {
URL url;
InputStream is = null;
BufferedReader br;
String line;
String result = null;
try {
url = new URL(urlXml);
is = url.openStream();
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
result = result + line + "\n";
}
} catch (Exception e) {
return "";
} finally {
try {
if (is != null) is.close();
} catch (IOException ioe) {}
}
return result;
}
Try this code
import java.net.*;
import java.io.*;
class Crawle {
public static void main(String ar[]) throws Exception {
URL url = new URL("http://www.foo.com/your_xml_file.xml");
InputStream io = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(io));
FileOutputStream fio = new FileOutputStream("file.xml");
PrintWriter pr = new PrintWriter(fio, true);
String data = "";
while ((data = br.readLine()) != null) {
pr.println(data);
}
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want city name from IP address using Java
is there any idea to do this ?
From Andrey link, here is how to construct the inquiry, this code will return an HTML file with all details of current IP, including city;
String IP= "123.123.123.123";
URL link = new URL("http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress="+IP);
BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
}
in.close();
UPDATE, 23 May 2013
The previous answer is ok, but it's not an API call, it reads an HTML page, that I provided previously because I didn't find any free APIs. Next is a REST API call that can be used easily and will return all the info required, it's recommended to use this one:
String ip = "2.51.255.200";
URL url = new URL("http://freegeoip.net/csv/" + ip);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
int status = connection.getResponseCode();
if (status != 200) {
return null;
}
reader = new BufferedReader(new InputStreamReader(is));
for (String line; (line = reader.readLine()) != null;) {
//this API call will return something like:
"2.51.255.200","AE","United Arab Emirates","03","Dubai","Dubai","","x-coord","y-coord","",""
// you can extract whatever you want from it
}
If your Application is deployed behind the firewall. So instead of calling a API, we can use GeoLite below is the sample code.
Download City.dat file from http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
File datapath = new File("GeoLiteCity.dat");
LookupService cl = new LookupService(datapath,
LookupService.GEOIP_MEMORY_CACHE
| LookupService.GEOIP_CHECK_CACHE);
String cityName = cl.getLocation(ipAddress).city;