I'm a beginner when it comes to coding and i would appreciate help with my code that is attempting to find a text file (info.txt) which exists on my computer. It should then print out the text in that file. This is what I have so far, but only to get the error that the file is not found...
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
public class demo1 {
static String filename = ("info.txt");
public static void main (String [] args) throws IOException{
FileReader from = new FileReader (fileName);
BufferedReader br = new BufferedReader (fr);
String currentLine;
while ((currentLine = br.readLine ()) != null){
System.out.println(currentLine);
}
}
}
I would greatly appreciate any input and thanks in advance for your help!
Related
Using the rx-java package, how can I tail a file?
code:
package tail;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Logger;
public class FileUtils {
public FileUtils() {
}
public void readFromInputStream(InputStream inputStream)
throws IOException {
StringBuilder resultStringBuilder = new StringBuilder();
try (BufferedReader br
= new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = br.readLine()) != null) {
resultStringBuilder.append(line).append("\n");
}
}
System.out.println(resultStringBuilder);
}
}
I'd want to parse each line as its written to the file. First, need an equivalent to tail -f foo.log from Java.
I'm not sure about using RxJava specifically. But you can take a general Java solution and combine with RxJava. This of course depends on how you want to be informed.
One basic option is to tail the file using an open InputStream (or some stream wrapper classes or reader class).
Using a running thread, you can wait on the file with InputStream.read or sample the amount of new bytes with InputStream.available. In RxJava you can use the IO scheduler. After finishing to read an entire line, just invoke some observers from the thread.
In core Java, it could look something like:
class ObserveTask implements Runnable {
private File file;
private Listener listener;
#Override
public void run() {
try (InputStream stream = new FileInputStream(file);
BufferedReader reader = new BufferedReader(stream)) {
while (!Thread.interrupted()) {
String line = reader.readLine();
listener.onNewLine(line);
}
}
}
}
Now just run the task:
Scheduler scheduler = Schedulers.newThread();
Scheduler.Worker worker = scheduler.createWorker();
worker.schedule(new ObserveTask());
Of course this is just a basic example.
You can split the task to parts, where each run, a check is made for the bytes, while the stream remains open. Of course this is dangerous, since leaving a stream open like that might end in leaking.
A more advanced solution can include using WatchService.
I want to create a Word document that uses different languages. In particular, I have a two-language original text where the language changes between English and German for each paragraph. This is what I tried:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
public class DocxCreator {
public static void createDocument(File docxOutput) throws IOException {
XWPFDocument doc = new XWPFDocument();
XWPFStyles docStyles = doc.createStyles();
docStyles.setSpellingLanguage("de-DE");
{
XWPFParagraph para = doc.createParagraph();
XWPFRun run = para.createRun();
run.setLanguage("de-DE"); // XXX: this method does not exist
para.setText("Deutsch");
}
{
XWPFParagraph para = doc.createParagraph();
XWPFRun paraRun = para.createRun();
para.setStyle("en-US");
paraRun.setText("English");
}
/*- XXX: How do I add the style “en-US” to the document and set its language to en-US”? */
/* XXX: How do I enable global grammar and spell checking? */
try (FileOutputStream fos = new FileOutputStream(docxOutput)) {
doc.write(fos);
}
}
public static void main(String[] args) throws IOException {
createDocument(new File("multilang.docx"));
}
}
I do not think this is currently supported by POI.
Generally, the language of the text is specified on the XWPFRun (XWPF) / CharacterRun (HWPF) level.
For HWPF (old binary *.doc format) there exists at least a method CharacterRun.getLanguageCode() - but no respective setter.
For XWPF (new *.docx format) I do not see such a thing at all.
The language codes are the same for *.doc and *.docx. A list is available here.
How do i convert a .doc file to FO using hwpf.converter.WordToFo class? I have tried searching but i could only get a word to html conversion.
I have also read the WordToFO manual at the apache-poi site, but could not get it.
Convert Word to HTML with Apache POI
I have tried to convert .doc to .fo using the following code, but after using apache-fop to convert the .fo file to .png, i am not able to get the images present in the word file.
package word2fo;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.converter.WordToFoConverter;
import org.apache.poi.hwpf.converter.WordToFoUtils;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.converter.WordToHtmlUtils;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.w3c.dom.Node;
public class Doc2Fo{
public static void main(String[] args) throws Exception {
System.out.println("reached 1");
HWPFDocumentCore wordDocument = WordToFoUtils.loadDoc(new FileInputStream("D:\\Magna.doc"));
System.out.println("reached 2");
WordToFoConverter wordToFoConverter = new WordToFoConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument());
System.out.println("reached 3");
wordToFoConverter.processDocument(wordDocument);
org.w3c.dom.Document htmlDocument = wordToFoConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource((Node) htmlDocument);
StreamResult streamResult = new StreamResult(out);
System.out.println("reached 4");
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer;
try {
serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
//serializer.setOutputProperty(OutputKeys.METHOD, "xml-fo");
serializer.transform(domSource, streamResult);
out.close();
String result = new String(out.toByteArray());
System.out.println(result);
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
HWPFDocument hwpfDocument = new HWPFDocument(POIDataSamples.getDocumentInstance().openResourceAsStream(sampleFileName));
WordToFoConverter wordToFoConverter = new WordToFoConverter(XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument());
wordToFoConverter.processDocument(hwpfDocument);
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(wordToFoConverter.getDocument()), new StreamResult(stringWriter));
String result = stringWriter.toString();
return result;
I am trying to write a java program which for loop the image files in a folder and
do reverse image search on google (just trying to do with one image in this question)
I find some example like below and the result will give me a url that preformed the reverse image search.
While I am wondering how can I download one of the image from the result site.
Some things to add is that I have read the new google api(?) and I find that it only allow me to do search 100/day, hence I choose to use the old version(?) although it warn me the method are deprecated.
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
public class ReverseImageSearch{
public static void main(String args[]){
try {
HttpClient client = new DefaultHttpClient();
String url="https://www.google.co.in/searchbyimage/upload";
String imageFile="C:\\Users\\Chan\\Desktop\\pixiv29706591.jpg";
HttpPost post = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
entity.addPart("encoded_image", new FileBody(new File(imageFile)));
entity.addPart("image_url",new StringBody(""));
entity.addPart("image_content",new StringBody(""));
entity.addPart("filename",new StringBody(""));
entity.addPart("h1",new StringBody("en"));
entity.addPart("bih",new StringBody("179"));
entity.addPart("biw",new StringBody("1600"));
post.setEntity(entity);
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
if (line.indexOf("HREF")>0)
System.out.println(line.substring(8));
//Problem:
// get one of the result image from the sites
// and store it in my PC
}
}catch (ClientProtocolException cpx){
cpx.printStackTrace();
}catch (IOException ioex){
ioex.printStackTrace();
}
}
}
I just started to develop a simple Blackberry app which shows a text sequence in a RichTextField on a MainScreen. When I define the String directly in the sourcecode, then I have no problem to display it. But if I try to read it in from a .txt file which is located in the res folder, then I get a NullPointerException.
The code below is what I did so far.
package mypackage;
import java.io.IOException;
import java.io.InputStream;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
public final class MyScreen extends MainScreen{
String str = readFile("Testfile.txt");
public MyScreen(){
setTitle("Read Files");
add(new RichTextField(str));
}
public String readFile(String filename){
InputStream is = this.getClass().getResourceAsStream("/"+filename);
try {
byte[] filebytes = IOUtilities.streamToBytes(is);
is.close();
return new String(filebytes);
}
catch (IOException e){
System.out.println(e.getMessage());
}
return "";
}
}
Parts of this code I found in this forum but my problem is that I don't understand when I have to open a connection and when to close it.
And when do I need a Buffer?
And why do I have to convert a InputStream to a byte[] and then the byte[] to a String?
All I need is one method, where I can type in the Filename and get back a String-Object with the text which is in my .txt file.
And of course the method should save resources...
package mypackage;
import java.io.IOException;
import java.io.InputStream;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
public final class MyScreen extends MainScreen {
public MyScreen() throws IOException {
setTitle("Read Files");
add(new RichTextField(readFileToString("Testfile.txt")));
}
public String readFileToString(String path) throws IOException {
InputStream is = getClass().getResourceAsStream("/"+path);
byte[] content = IOUtilities.streamToBytes(is);
is.close();
return new String(content);
}
}
Yes!!! I found a way to solve my problem.
I don't know why my previous code didn't work but this one works...
The only thing I've changed is that I've added the throws IOException instead of surrounding it with a try - catch block...