I am having trouble with parsing some html files from a directory into an output directory. I'm using Jsoup to remove HTML tags and writing to an output directory but some of the data is lost when I'm testing it. What I want to do in the end with the parsed files is to populate a hashmap so that I can sort the words by frequency and then in a separate directory sort them by alphabetical order. This compiles and runs, but I am getting stuck at the very end when it comes to write out. Code would be lovely and all, but I'm only interested in the steps to take in order to set this entire thing up. Thank you.
Update: Here is code.
Update: Also I feel like Jsoup is getting rid of data.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import org.jsoup.Jsoup;
public class Parser {
public static File infolder = new File("input folder folder path goes here");
static String temp = "";
static ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args) throws FileNotFoundException
{
String outfolder = "output folder path goes here";
File theDir = new File(outfolder);
// if the directory does not exist, create it
if (!theDir.exists()) {
System.out.println("creating directory: " + outfolder);
boolean result = theDir.mkdir();
if (result) {
System.out.println("DIR created");
}
}
System.out.println("Reading files under the folder " + infolder.getAbsolutePath());
parseFiles(infolder);
// System.out.println();
}
public static void parseFiles(final File folder) throws FileNotFoundException
{
PrintWriter out = null;
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isFile()) {
temp = fileEntry.getName();
if ((temp.substring(temp.lastIndexOf('.') + 1, temp.length()).toLowerCase()).equals("html")) {
System.out.println("File= " + folder.getAbsolutePath() + "\\" + fileEntry.getName());
File file = new File(folder.getAbsolutePath() + "\\" + fileEntry.getName());
ArrayList<String> filetext = new ArrayList<String>();
Scanner in = new Scanner(file);
while (in.hasNextLine()) {
filetext.add(in.nextLine());
}
String filename = "tokenfile" + fileEntry.getName();
try {
out = new PrintWriter(new BufferedWriter(new FileWriter("C:/Users/bounty213/Desktop/Output/" + filename + ".txt", true)));
}
catch (IOException e) {
//exception handling left as an exercise for the reader
}
String parsed;
for (String word : filetext) {
parsed = Jsoup.parse(word).text();
System.out.println(parsed);
out.println(parsed);
}
out.close();
}
}
}
}
}
Related
Read the CSV file and how to format it,
I'm trying to use Selenium with Java, to upload this file to the search fields, but this test is to show how the website will read the CSV file
package utiliites;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CSV {
// This method will read and return data from a CSV file
public static List<String[]> get(String filename) {
List<String[]> data = new ArrayList<String[]>();
String testRow;
try {
// Open and read the file
BufferedReader br = new BufferedReader(new FileReader(filename));
// Read data as long as it's not empty
// Parse the data by comma using .split() method
// Place into a temporary array, then add to List
while ((testRow = br.readLine()) != null) {
String[] line = testRow.split(",");
data.add(line);
}
} catch (FileNotFoundException e) {
System.out.println("ERROR: File not found " + filename);
} catch (IOException e) {
System.out.println("ERROR: Could not read " + filename);
}
return data;
}
}
Read the CSV and here where is it showing like this (???)
package Test;
import java.util.List;
public class DataReadrs {
public static void main(String[] args) {
readCSV();
}
private static void readCSV() {
String filename = "C:\\Users\\user\\Downloads\\Delegation(IdealLandData).csv";
List<String[]>records = utiliites.CSV.get(filename);
for (String[] record : records) {
for (String field : record) {
System.out.println((record[0]));
}
}
}
}
I am creating an Android module in react-native
I never worked with Java or writing code in Java
How can I complete the code below?
What I want
1- look and verify if the directory exist if it exist then remove it.
2- recreate the directory.
3- create a json file and add its content.
Here is what I got so far
#ReactMethod
public string write(string content) {
var folder = "NovelManager";
File path = Paths.get(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), folder);
var fullPath = Paths.get(path, "NovelManager.backup.json");
makeDir(path);
File file = new File(path, "NovelManager.backup.json");
if (!file.exists())
file = file.createNewFile();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
out.write(content);
out.close();
return file.getAbsolutePath();
}
private void makeDir(string dirPath){
var dir = new File(dirPath);
if (!dir.exists())
dir.mkdir();
}
Update and solution
After to much hard work this did thing for me.
Here is the complete code for anyone who have similar problem.
// DownloadFileModule.java
package com.novelmanager;
import android.view.View;
import android.app.Activity;
import java.io.BufferedWriter;
import java.io.Console;
import java.io.File;
import java.io.FileWriter;
import android.os.Environment;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
public class DownloadFileModule extends ReactContextBaseJavaModule {
#Override
public String getName() {
return "DownloadFileModule";
}
#ReactMethod(isBlockingSynchronousMethod = true)
public String write(String content) {
if (content == null || content == "")
return "";
try {
String folder = "NovelManager";
String fileName = "NovelManager.backup.json";
String downloadFolderPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.getPath();
String dirPath = compine(downloadFolderPath, folder);
File dir = new File(dirPath);
if (!dir.exists())
dir.mkdir();
String path = compine(downloadFolderPath, folder, fileName);
File file = new File(path);
if (!file.exists())
file.createNewFile();
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
out.write(content);
out.close();
return file.getPath();
} catch (Exception e) {
return e.getMessage();
}
}
private String compine(String... more) {
String url = more[0];
for (int i = 1; i < more.length; i++) {
String str = more[i];
if (str.startsWith("/"))
str = str.substring(1);
if (str.endsWith("/"))
str = str.substring(0, str.length() - 1);
if (url.endsWith("/"))
url = url.substring(0, url.length() - 1);
url = url + "/" + str; // relative url
}
return url; // relative url
}
DownloadFileModule(ReactApplicationContext reactContext) {
super(reactContext);
}
}
To delete directory
public boolean deleteDirectory(Path pathToBeDeleted) throws IOException {
Files.walk(pathToBeDeleted)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
return !Files.exists(pathToBeDeleted);
}
To write to file
public void writeToFile(String content, File file) throws IOException {
Files.write(file.toPath(), content.getBytes());
}
You can use Apache FileUtils to perform all the required operations for e.g.
Reference : https://commons.apache.org/proper/commons-io/javadocs/api-2.5/index.html?org/apache/commons/io/FileUtils.html
FileUtils.cleanDirectory(path); //clean out directory (this is optional)
FileUtils.forceDelete(path); //delete directory
FileUtils.forceMkdir(path); //create directory
FileUtils.touch(file)); //create new file
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("\'", "'");
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.
I am new to java, but not coding. I am trying to figure out java because it's part of my class this term and I am having a really hard problem grasping the idea of it and implementing things in java.
my problem Is that I am not sure if I am correctly using the arraylist to grab data from the scan of the file and input it into a arraylist to sort and print at a later time. I am just having issues picking up on java any help would be great since I am new to java.
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.*;
public class MissionCount
{
private static ArrayList<String> list = new ArrayList<String>();
// returns an InputStream that gets data from the named file
private static InputStream getFileInputStream(String fileName) throws Exception {
InputStream inputStream;
try {
inputStream = new FileInputStream(new File(fileName));
}
catch (FileNotFoundException e) { // no file with this name exists
inputStream = null;
throw new Exception("unable to open the file -- " + e.getMessage());
}
return inputStream;
}
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("USage: MissionCount <datafile>");
//System.exit(1);
}
try {
System.out.printf("CS261 - MissionCount - Chad Dreher%n%n");
int crewcount = 0;
int misscount = 0;
InputStream log = getFileInputStream(args[0]);
Scanner sc = new Scanner(log);
sc.useDelimiter(Pattern.compile(",|\n"));
while (sc.hasNext()) {
String crewMember = sc.next();
list.add(crewMember);
String mission = sc.next();
list.add(mission);
}
sc.close();
// Add code to print the report here
}catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
InputStream log = getFileInputStream(args[0]);
Change that line to as follows :-
File log = new File(args[0])
that should work!
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);
}
}