Save the file with the name of the site - java

The following code saves the page's content to a file:
import java.net.*;
import java.io.*;
public class url
{
public static void main(String[] args)
{
try
{
URL PageUrl;
URLConnection GetConn = null;
GetConn = null;
PageUrl = new URL("https://www.google.ru/");
GetConn = PageUrl.openConnection();
GetConn.connect();
InputStreamReader ReadIn = new InputStreamReader(GetConn.getInputStream());
BufferedReader BufData = new BufferedReader(ReadIn);
String htmlFileName = ("C:\\hello.html");
FileWriter FWriter = new FileWriter(htmlFileName);
BufferedWriter BWriter = new BufferedWriter(FWriter);
String UrlData = null;
while ((UrlData = BufData.readLine()) != null)
{
BWriter.write(UrlData);
BWriter.newLine();
}
BWriter.close();
}
catch(IOException io)
{
System.out.println(io);
}
}
}
But I need the file have the same name as the page of the website, for example, it has to somehow get the name of the web page and assign it as the file's name.

You can use URL.getFile() to get the filename. I.e
...
String htmlFileName = PageURL.getFile();
...
Note that different URLs may refer to the same file: http://example.com/test.html#anch1, http://example.com/test.html, http://example.com/test.html?a=b - all three refer to the same test.html file here. In this case you might want to combine getFile(), getRef() and getQuery() somehow.
It is worth to mention some issues in your code:
start variable names with lowerCase instead of UpperCase;
Close resources in finally blocks. Better, if you use Java 7, use try-with-resources.

Related

reading text from a relative file path

I have an absolute file path in my java program that contains some text. This is the code:
import java.io.*;
import java.util.*;
public class RoughCode {
public static void main(String[] args) {
try {
File rules=new File("C:\\Users\\Owner\\Documents\\ICS4U\\Assignment 1\\GameShowRules.txt");
Scanner scan=new Scanner(rules);// scans the file 'rules'
while (scan.hasNextLine()) {
System.out.println(scan.nextLine());// outputs 'rules' to console
}
}
catch(Exception e) {
System.out.println("File not found");
System.exit(0);
}
}
}
Here, the code works just fine. The output I get is whatever is stored in the file, which is:
The rules of the game are:
You must answer 15 multiple-choice questions correctly in a row to win the jackpot.
You may quit at any time and keep the earnings.
However, what I need is a relative file path so that it runs on any laptop.
In an attempt to do that, I did:
import java.io.*;
import java.net.URI;
import java.util.*;
public class RoughCode {
public static void main(String[] args) {
try {
// Two absolute paths
File absolutePath1 = new File("C:\\Users\\Owner\\Documents\\ICS4U\\Assignment 1\\GameShowRules.txt");
File absolutePath2 = new File("C:\\Users\\Owner\\Documents\\ICS4U\\Assignment 1");
// convert the absolute path to URI
URI path1 = absolutePath1.toURI();
URI path2 = absolutePath2.toURI();
// create a relative path from the two paths
URI relativePath = path2.relativize(path1);
// convert the URI to string
String path = relativePath.getPath();
Scanner scan=new Scanner(path);
while (scan.hasNextLine()) {
System.out.println(scan.nextLine());
}
}
catch(Exception e) {
System.out.println("File not found");
System.exit(0);
}
}
}
This does not display the text I need. it just displays "GameshowRules.txt".
How do I get it to output the text stored in the file?
Thanks
Try to use BufferedReader and FileReader. My "data.txt" file is in the same folder as the java program, and works just fine.
I guess you know where will be file of your own program, so you can paste relative path to it.
It looks like this
public class Project {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
String data;
while ((data = reader.readLine()) != null) {
System.out.println(data);
}
}
}

How can I compile a Java class stored in a string, or using it's path with a given input?

I'm trying to make my own pretty print for java files, similar to JDoodle. How can I compile a java class, given either it's location as a string, or its content as a string, as well as do it given a text file for std inputs, all the while recording the output as a seperate string. Sorry if this seems troublesome. Any help is appreciated!
EDIT: I do know about the java.tools.ToolProvider and Tool, but even if it is the solution, I don't know what to do with it, as the documentation is too confusing for me, or too sparse.
OK, I got an answer. I used Eclipse's compiler(cause I dont have JDK in my school laptop) to compile and used processbuilder to run the produced .class file, redirected the output using redirectOutput to a file which I read to get the output. Thanks- Here is the code.
/*PRETTYPRINT*/
/*
* Code to HTML
* Uses highlightjs in order to create a html form for your code, you can also give inputs and outputs
* */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
public class PrettyPrint {
public static void main(String[] args) throws FileNotFoundException{
String javaFile = readFile(args[0]);
String commandLine = readFile(args[1]);
String output = readFile(args[2]);
String html = "<!DOCTYPE html>\n"
+"<html>\n"
+"<head>"
+"<link rel=\"stylesheet\" href=\"highlightjs/styles/a11y-dark.css\" media= \"all\">\r\n"
+"<script src=\"highlightjs/highlight.pack.js\"></script>\r\n"
+"<script>hljs.initHighlightingOnLoad();</script>"
+"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js\" integrity=\"sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/\" crossorigin=\"anonymous\"></script>\r\n"
+"<script src=\"https://cdn.jsdelivr.net/npm/html2canvas#1.0.0-rc.5/dist/html2canvas.min.js\"></script>"
+"<meta charset=\"utf-8\">"
+"<style>code{overflow-x: visible;}body{background-color:#888888;color:#444444;}h1{text-align:center;color:#444444;}</style>"
+"</head>"
+"<body style=\"font-family: 'Consolas';\">\n"
+"<h1 style=\"text-align: center\">Java Code</h1>"
+"<pre><code class=\"java\" style=\"overflow-x:visible\">"
+toHTML(javaFile)
+"</code></pre>"
+"<br>\n"
+"<h1>Inputs</h1>"
+"<pre><code class = \"nohighlight hljs\" style=\"overflow-x:visible\">"
+toHTML(commandLine)
+"</code></pre>"
+"<br>\n"
+"<h1>Output</h1>"
+"<pre><code class = \"nohighlight hljs\" style=\"overflow-x:visible\">"
+toHTML(output)
+"</code></pre>"
+"</body>\n"
+"<script>"
+"console.log(document.body.innerHTML);"
//+String.format("function print(){const filename='%s';html2canvas(document.body).then(canvas=>{let pdf = new jsPDF('p','mm', 'a4');pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 1000, 1000);pdf.save(filename);});}print();",args[3].substring(args[3].lastIndexOf('/')+1, args[3].length()-4)+"pdf")
+ "</script>"
+"</html>\n";
//System.out.println(html);
try {
File file = new File("output.html");
PrintWriter fileWriter = new PrintWriter(file);
fileWriter.print(html);
fileWriter.close();
} catch(IOException e) {
e.printStackTrace();
}
}
public static String toHTML(String str) {
String html = str;
html = html.replace("&","&");
html = html.replace("\"", """);
html = html.replace("\'", "&apos;");
html = html.replace("<", "<");
html = html.replace(">", ">");
//html = html.replace("\n", "<br>");
html = html.replace("\t", "  ");
html+= "<br>";
return html;
}
public static String readFile(String filePath)
{
String content = "";
try
{
content = new String ( Files.readAllBytes( Paths.get(filePath) ) );
}
catch (IOException e)
{
e.printStackTrace();
}
return content;
}
}
/**PROCESSBUILDEREXAMPLE**/
import java.io.*;
import org.eclipse.jdt.core.compiler.CompilationProgress;
import org.eclipse.jdt.core.compiler.batch.BatchCompiler;
public class ProcessBuilderExample {
private static String JAVA_FILE_LOCATION;
public static void main(String args[]) throws IOException{
JAVA_FILE_LOCATION = args[0];
CompilationProgress progress = null;
BatchCompiler.compile(String.format("-classpath rt.jar %s",args[0]), new PrintWriter(System.out), new PrintWriter(System.err), progress);
Process process = new ProcessBuilder("java", "-cp",
JAVA_FILE_LOCATION.substring(0,JAVA_FILE_LOCATION.lastIndexOf("\\")),
JAVA_FILE_LOCATION.substring(JAVA_FILE_LOCATION.lastIndexOf("\\")+1,JAVA_FILE_LOCATION.length()-5))
.redirectInput(new File(args[1]))
.redirectOutput(new File(args[2])).start();
try {
process.waitFor();
PrettyPrint.main(args);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Keep these 2 in the same folder and run processbuilderexample with 3 arguments. The code's loc, the input file's loc, and the output file to write to.

Want to read content from two text files then compare and write to another text file with java

I have text files.
album.txt
new_album.txt
Each text files contains some folder name.
For example,
album.txt contains
#Event1
#Event2
#Event3
and new_album.txt contains
#Event1(update20-05-2015)
#Event2(update03-03-2016)
#Event3(update15-08-2016)
#Event4(update30-07-2017)
I want to compare similar folder name from album.txt with new_album.txt line by line, then put folder name that similar from album.txt to similar.txt and put folder name that not match to not_match.txt .
Output in similar.txt
#Event1
#Event2
#Event3
Output in not_match.txt
#Event4(update30-07-2017)
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CompareFileName {
public static void main(String[] args) throws Exception {
BufferedReader br1 = null;
BufferedReader br2 = null;
String sCurrentLine;
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
br1 = new BufferedReader(new FileReader("album.txt"));
br2 = new BufferedReader(new FileReader("new_album.txt"));
while ((sCurrentLine = br1.readLine()) != null) {
list1.add(sCurrentLine);
}
while ((sCurrentLine = br2.readLine()) != null) {
list2.add(sCurrentLine);
}
//This part is my problem
List <String> list_similar = new ArrayList<String>();
List <String> list_not_match = new ArrayList<String>();
for (String string : list1) {
if(string.matches("list2")){ //I don't know how to compare similar folder name from list2 with list1.
list_similar.add(string);
}else{list_not_match.add(string)}
}
//This part is the part I use for add string to text file but it not complete I want to write string from list_similar to similar.text and list_not_match to not_match.txt
file = new File("similar.txt");
fileName = "similar.txt";
str = file.list();
try{
PrintWriter outputStream = new PrintWriter(fileName);
for(String string:str){
outputStream.println(string);
}
outputStream.close();
System.out.println("get name complete");
}
catch (FileNotFoundException e){
e.printStackTrace();
}
System.out.println("done.");
}
If you want to read something from files, you can use these streams.
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(new File("path\\to\\your\\file.txt"))); //or any format
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("path\\to\\your\\second\\file.txt")));
//read one line from your file
String line = reader.readLine();
//write something to your file
writer.write(line);
}
If you want to read folder names you can use this.
File f = f = new File("path\\to\\your\\folder\\with\\files");
File[] files = f.listFiles();
for(File currentFile : files) {
System.out.println(currentFile.getName());
}
If you want to create new files or folders, you can use this.
File f = f = new File("path\\to\\your\\folder\\with\\files");
f.mkdir();
//or
f.mkdirs();
//or if you have File f = new File("myTextFile.txt"); then you can create file using this:
f.createNewFile();
Application (main method)
public class Application {
public static void main(String... aArgs) throws IOException {
InputParser firstListInputParser = new InputParser(new File(/**"Your path to /album.txt"*/));
firstListInputParser.processLineByLine();
List<String> firstList = firstListInputParser.getListWithParsedFolderNames();
firstListInputParser.printMap();
InputParser secondListInputParser = new InputParser(new File(/**"Your path to /new_album.txt"*/));
secondListInputParser.processLineByLine();
List<String> secondList = secondListInputParser.getListWithParsedFolderNames();
secondListInputParser.printMap();
// Create the list with common value and write it to the file
List<String> listWithCommonValues = new ArrayList<String>(firstList);
listWithCommonValues.retainAll(secondList);
Path fileCommon = Paths.get(/**"Your path to /similar.txt"*/);
Files.write(fileCommon, listWithCommonValues, Charset.forName("UTF-8"));
// Create the list with different values and write it to the file
List<String> listWithAllValues = new ArrayList<String>(firstList);
listWithAllValues.addAll(secondList);
//remove the common values from the list with all values
listWithAllValues.removeAll(listWithCommonValues);
Path fileDistincts = Paths.get(/**"Your path to /not_match.txt"*/);
Files.write(fileDistincts, listWithAllValues, Charset.forName("UTF-8"));
}
private static void log(Object aObject){
System.out.println(String.valueOf(aObject));
}
}
Inputparser
/**
* Assumes UTF-8 encoding
*/
public class InputParser {
//create a list to hold the values
List<String> listWithParsedFolderNames = new ArrayList<>();
//private final Path fFilePath;
private final File file;
private final static Charset ENCODING = StandardCharsets.UTF_8;
/**
Constructor.
#param aFileName full name of an existing, readable file.
*/
public InputParser(File aFileName){
//fFilePath = Paths.get(aFileName);
file = aFileName;
}
/**
* Processes each line and calls {#link #processLine(String)}}.
*/
public final void processLineByLine() throws IOException {
try (Scanner fileScanner = new Scanner(file, ENCODING.name())){
while (fileScanner.hasNextLine()){
processLine(fileScanner.nextLine());
}
}
}
/**
Overridable method for processing lines in different ways.
*Parses the line and cuts away the part after '(update'
* Ex1: input line: #Event1(update20-05-2015)
* Ex1: output : #Event1
*
* Ex2: input line: #Event2
* Ex2: output : #Event2
*/
protected void processLine(String aLine){
Scanner scanner = new Scanner(aLine);
if (scanner.hasNext()) {
String name = scanner.next();
String finalName = name.split("\\(update")[0];
//stores the values in the list
listWithParsedFolderNames.add(finalName);
} else {
log("Empty or invalid line. Unable to process.");
}
}
/**
* Prints the content of the listWlistWithParsedFolderNames
*/
public void printMap() {
Iterator it = listWithParsedFolderNames.iterator();
while (it.hasNext()) {
log("The prsed value is: " + it.next());
}
}
/**
* #return the list with values
*/
public List<String > getListWithParsedFolderNames() {
return this.listWithParsedFolderNames;
}
private static void log(Object aObject){
System.out.println(String.valueOf(aObject));
}
}
In the similar.txt it will print:
#Event1
#Event2
#Event3
In the not_match.txt it will print:
#Event4
If you want it to print #Event4(update30-07-2017) into the not_match class you will have to change the list to a key value map that has the parsed input #Event4 as key and the full line #Event4(update30-07-2017) as value. After comparing the keys of the map you can write the values into your file.

how to read directories in java

simple: how do i read the contents of a directory in Java, and save that data in an array or variable of some sort? secondly, how do i open an external file in Java?
You can use java IO API. Specifically java.io.File, java.io.BufferedReader, java.io.BufferedWriter etc.
Assuming by opening you mean opening file for reading. Also for good understanding of Java I/O functionalities check out this link: http://download.oracle.com/javase/tutorial/essential/io/
Check the below code.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileIO
{
public static void main(String[] args)
{
File file = new File("c:/temp/");
// Reading directory contents
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
// Reading conetent
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("c:/temp/test.txt"));
String line = null;
while(true)
{
line = reader.readLine();
if(line == null)
break;
System.out.println(line);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(reader != null)
{
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
You can use a class java.io.File to do that. A File is an abstract representation of file and directory pathnames. You can retrieve the list of files/directories within it using the File.list() method.
There's also the Commons IO package which has a variety of methods for manipulating files and directories.
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
public class CommonsIO
{
public static void main( String[] args )
{
// Read the contents of a file into a String
try {
String contents = FileUtils.readFileToString( new File( "/etc/mtab" ) );
} catch (IOException e) {
e.printStackTrace();
}
// Get a Collection of files in a directory without looking in subdirectories
Collection<File> files = FileUtils.listFiles( new File( "/home/ross/tmp" ), FileFilterUtils.trueFileFilter(), null );
for ( File f : files ) {
System.out.println( f.getName() );
}
}
}
public class StackOverflow {
public static void main(String[] sr) throws IOException{
//Read a folder and files in it
File f = new File("D:/workspace");
if(!f.exists())
System.out.println("No File/Dir");
if(f.isDirectory()){// a directory!
for(File file :f.listFiles()){
System.out.println(file.getName());
}
}
//Read a file an save content to a StringBuiilder
File f1 = new File("D:/workspace/so.txt");
BufferedReader br = new BufferedReader(new FileReader(f1));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null)
sb.append(line+"\n");
System.out.println(sb);
}
}

How to read a text-file resource into Java unit test?

I have a unit test that needs to work with XML file located in src/test/resources/abc.xml. What is the easiest way just to get the content of the file into String?
Finally I found a neat solution, thanks to Apache Commons:
package com.example;
import org.apache.commons.io.IOUtils;
public class FooTest {
#Test
public void shouldWork() throws Exception {
String xml = IOUtils.toString(
this.getClass().getResourceAsStream("abc.xml"),
"UTF-8"
);
}
}
Works perfectly. File src/test/resources/com/example/abc.xml is loaded (I'm using Maven).
If you replace "abc.xml" with, say, "/foo/test.xml", this resource will be loaded: src/test/resources/foo/test.xml
You can also use Cactoos:
package com.example;
import org.cactoos.io.ResourceOf;
import org.cactoos.io.TextOf;
public class FooTest {
#Test
public void shouldWork() throws Exception {
String xml = new TextOf(
new ResourceOf("/com/example/abc.xml") // absolute path always!
).asString();
}
}
Right to the point :
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());
Assume UTF8 encoding in file - if not, just leave out the "UTF8" argument & will use the
default charset for the underlying operating system in each case.
Quick way in JSE 6 - Simple & no 3rd party library!
import java.io.File;
public class FooTest {
#Test public void readXMLToString() throws Exception {
java.net.URL url = MyClass.class.getResource("test/resources/abc.xml");
//Z means: "The end of the input but for the final terminator, if any"
String xml = new java.util.Scanner(new File(url.toURI()),"UTF8").useDelimiter("\\Z").next();
}
}
Quick way in JSE 7
public class FooTest {
#Test public void readXMLToString() throws Exception {
java.net.URL url = MyClass.class.getResource("test/resources/abc.xml");
java.nio.file.Path resPath = java.nio.file.Paths.get(url.toURI());
String xml = new String(java.nio.file.Files.readAllBytes(resPath), "UTF8");
}
Quick way since Java 9
new String(getClass().getClassLoader().getResourceAsStream(resourceName).readAllBytes());
Neither intended for enormous files though.
First make sure that abc.xml is being copied to your output directory. Then you should use getResourceAsStream():
InputStream inputStream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("test/resources/abc.xml");
Once you have the InputStream, you just need to convert it into a string. This resource spells it out: http://www.kodejava.org/examples/266.html. However, I'll excerpt the relevent code:
public String convertStreamToString(InputStream is) throws IOException {
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
With the use of Google Guava:
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
public String readResource(final String fileName, Charset charset) throws Exception {
try {
return Resources.toString(Resources.getResource(fileName), charset);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
Example:
String fixture = this.readResource("filename.txt", Charsets.UTF_8)
You can try doing:
String myResource = IOUtils.toString(this.getClass().getResourceAsStream("yourfile.xml")).replace("\n","");
Here's what i used to get the text files with text. I used commons' IOUtils and guava's Resources.
public static String getString(String path) throws IOException {
try (InputStream stream = Resources.getResource(path).openStream()) {
return IOUtils.toString(stream);
}
}
You can use a Junit Rule to create this temporary folder for your test:
#Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
File file = temporaryFolder.newFile(".src/test/resources/abc.xml");
OK, for JAVA 8, after a lot of debugging
I found that there's a difference between
URL tenantPathURI = getClass().getResource("/test_directory/test_file.zip");
and
URL tenantPathURI = getClass().getResource("test_directory/test_file.zip");
Yes, the / at the beginning of the path without it I was getting null!
and the test_directory is under the test directory.
Using Commons.IO, this method works from EITHER a instance method or a static method:
public static String loadTestFile(String fileName) {
File file = FileUtils.getFile("src", "test", "resources", fileName);
try {
return FileUtils.readFileToString(file, StandardCharsets.UTF_8);
} catch (IOException e) {
log.error("Error loading test file: " + fileName, e);
return StringUtils.EMPTY;
}
}

Categories

Resources