java file reader weird output - java

I set up a FileReader, and opened a file to read, but it gives me a weird output, that I can't seem to fix:
import java.io.BufferedReader;
import java.io.FileReader;
public class FileReading {
public static void main(String [] args) throws Exception {
FileReader file = new FileReader("/Users/danielpersonius/Desktop/test.rtf");
BufferedReader reader = new BufferedReader(file);
String text = "";
String line = reader.readLine();
while (line != null){
// So here, we want to print until it reaches 'null'
text += line;
line = reader.readLine();
}
System.out.println(text);
}
}
This is my output:
{\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf200{\fonttbl\f0\fswiss\fcharset0
Helvetica;}{\colortbl;\red255\green255\blue255;}\margl1440\margr1440\vieww10800\viewh8400\viewkind0\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\f0\fs24
\cf0 TEST}
TEST is what the rtf file says, but how do I get rid of all the other stuff I obviously don't want?
I'm on an IMac with OS X Mavericks

The problem is that your are probably creating your file in TextEdit. TextEdit does not save files as a raw text file. Instead it saves it in a RTF (Rich Text File) format which embeds formating commands. You need to use a text editor that can create an ASCII text file.
For more information on RTF.

Just use the same editor you use to write your code to create your "test.*" file :)

On your test file go to format in the toolbar, then click convert to .txt file for conversion.
This change would get rid of the weird output

Related

Running multiple PDF through an PDFBox program

Currently I am trying to use PDFBox in Eclipse to run multiple PDF files in a folder through a text reader that will extract certain terms and output them into a text file that I will then convert to an excel sheet. Currently I have the program and it works correctly for a single PDF file:
public static void main(String args[]) throws IOException {
//Loading an existing document
File file = new File("ADE_acetylfuranoside_120319_pfister.pdf");
PDDocument document = PDDocument.load(file);
//Instantiate PDFTextStripper class
PDFTextStripper pdfStripper = new PDFTextStripper();
//Retrieving text from PDF document
String text = pdfStripper.getText(document);
//..."Actual code that extracts text"...
PrintStream o = new PrintStream(new File("output.txt"));
PrintStream console = System.out;
System.setOut(o);
System.out.println(finalSheet);
my problem is that I want to run 500 PDFs in one folder through this program on eclipse rather than putting in the name of each one individually. I also want it to output like:
Name1, Number1, ID1
Name2, Number2, ID2
but I think the way it is written now it will just overwrite line number one if I run multiple PDFs though it.
Thanks for the help!
For the first part, you could just use the File class with a FileFilter:
// directoryName could be as simple a "."
File folder = new File(directoryName);
File[] listOfFiles = folder.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.getName().toLowerCase().endsWith(".pdf");
}
});
This gives you an array of File objects of all the files in a particular folder/directory. Now you can loop through it with pretty much the code you have.
On the output side, you'll likely want to correlate the output with the input. I'm a bit confused by your code and I'm guessing you'd just like an output file for each input file. So, perhaps, something like:
// index is the value you used to loop through the `listOfFiles` array
try( FileWriter fileWriter = new FileWriter(listOfFiles[index].getName() + ".output.txt" ) ) {
fileWriter.write( // the String text you want in the file );
}
This creates a file named (as taken from your example) "ADE_acetylfuranoside_120319_pfister.pdf.output.txt". Obviously this could change. In this case a new file is created for each input file.

Displaying .png files in java

I can display the text contained a .txt or .rtf file in java using this,
import java.io.*;
class test
{
public static void main(String args[])throws Exception
{
File f=new File("C:\\Users\\shinj\\Documents\\LIVE\\BEGINNER.txt");
String path = f.getAbsolutePath();//Pass file's absolute path
BufferedReader reader = new BufferedReader(new FileReader(path));
String line =null;
for (int i =1;i<=14;i++)
{
line=reader.readLine();
System.out.println(line);
}
}
}
But the same doesn't happen for any .png files.
What I want to know is that is there any way to display the contents of a .png files in the terminal output screen of java.
For example, if there is a picture of a stickman in such a file, I want to display the stickman on the terminal window.
Most of terminals can display only text data but some terminals allow to display emoji characters and images. For example it is possible in iTerm for macOS.

Accessing a file in a runnable JAR [duplicate]

This question already has answers here:
Java resource as File
(6 answers)
Closed 9 years ago.
What I am attempting to do is store a text file (that won't change) inside the JAR of the program so that it can be read. The purpose of the text file is that it will be read in by one of my classes and the contents of the text file will be added to an JEditorPane. The file will basically be a tutorial and when the user clicks on the option to read the tutorial, the file contents will be read and displayed in a new window that pops up.
I have the GUI portion of it down, but as far as storing the file in the JAR so it can be accessed, I am at a lost. I've read that using an InputStream will work, but after trying a few things I haven't gotten it to work yet.
I also store images in the JAR to be used as icons for the GUI windows. This is accomplished with:
private Image icon = new ImageIcon(getClass()
.getResource("resources/cricket.jpg")).getImage();
But, this doesn't work when trying to get a file:
private File file = new File(getClass.getResource("resources/howto.txt"));
Here is my Class as it is now:
public class HowToScreen extends JFrame{
/**
*
*/
private static final long serialVersionUID = -3760362453964229085L;
private JEditorPane howtoScreen = new JEditorPane("text/html", "");
private Image icon = new ImageIcon(getClass().getResource("resources/cricket.jpg")).getImage();
private BufferedReader txtReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/howto.txt")));
public HowToScreen(){
setSize(400,300);
setLocation(500,200);
setTitle("Daily Text Tutorial");
setIconImage(icon);
howtoScreen.setEditable(false);
howtoScreen.setText(importFileStream());
add(howtoScreen);
setVisible(true);
}
public String importFile(){
String text = "";
File file = new File("howto.txt");
Scanner in = null;
try {
in = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while(in.hasNext()){
text += in.nextLine();
}
in.close();
return text;
}
public String importFileStream(){
String text = "";
Scanner in = new Scanner(txtReader);
while(in.hasNext()){
text += in.nextLine();
}
in.close();
return text;
}
}
Ignore the importFile method as that is being removed in favor of storing the tutorial file inside the JAR, making the program wholly self contained as I am limited to how much space the program can use.
EDIT:
After trying all of the suggestions below, I checked to see if my JAR is packaging the text file in it and it is not. When opening the JAR with 7zip, in my resources folder the picture I use for icons is there, but not the text file.
You cannot use File inside a JAR file. You need to use InputStream to read the text data.
BufferedReader txtReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/mytextfile.txt")));
// ... Use the buffered reader to read the text file.
Try the next (with the full path package):
InputStream inputStream = ClassLoader.getSystemClassLoader().
getSystemResourceAsStream("com/company/resources/howto.txt");
InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader in = new BufferedReader(streamReader);
for (String line; (line = in.readLine()) != null;) {
// do something with the line
}
You code will not compile. Class.getResource() returns a URL, and File has no constructor with a URL as an argument.
You can just use .getResourceAsStream() instead, it returns an InputStream directly, you just have to read the contents of the file from that stream.
Note: both of these methods return null if the resource is not found: don't forget to check for that...
the contents of the text file will be added to an JEditorPane.
See DocumentVewer & especially JEditorPane.setPage(URL).
Since the help is an embedded-resource it will be necessary to gain an URL using getResource(String) as detailed in the info. page.
.. tried this: URL url = this.getClass().getResource("resources/howto.txt");
Change:
URL url = this.getClass().getResource("resources/howto.txt");
To:
URL url = this.getClass().getResource("/resources/howto.txt"); // note leading '/'

Reading from a file in Java

I wrote a program that reads from a text file using Java. The file has 1 column with a lot of integer values and each value is being added to an array list. However, when I print the array list, between each number I am getting an empty entry. For example if in text file I have:
4
55
I am getting:
1 : ÿþ4 (Also I do not know what this weird character is)
2 :
3 : 555
Code:
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class ReadFile {
public static void main(String[] args) {
try
{
Scanner input = new Scanner("ReadingFile.txt");
File file = new File(input.nextLine());
input = new Scanner(file);
ArrayList numbers = new ArrayList();
int i=1;
while (input.hasNextLine()) {
String line = input.nextLine();;
numbers.add(line);
System.out.println(i + " : " + line);
i++;
}
input.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
I tried to avoid using the arraylist and just do :
System.out.println(i + " " + line);
however this problem is still there so I am guessing that it is not an ArrayList problem.
Provided your text file is actually a good text file, it could be a character encoding thing. You need to provide the correct character set to your scanner in its constructor. So change the line:
input = new Scanner(file);
Into something like:
String charset = "UTF-8";
input = new Scanner(file, charset);
Ofcourse, you need to figure out which character set your file is actually stored as and use that one. I do UTF-8 here only as an example.
OK, the problem is that you're actually reading binary from an excel file, hence the strange characters. If you want to read an excel file directly, then use a library such as JXL (http://jexcelapi.sourceforge.net/) - here's a good tutorial for using that API: http://www.vogella.com/tutorials/JavaExcel/article.html
Otherwise, you would want to save export your excel file to CSV format and read the file with your code.
weird chars should be writeUTF prefix or BOM. so, depends on how you write the file, reading method can be different.
if you write file with DataOutputStream and call writeUTF, then you should read the file with readUTF
if it is a simple text file that was written by a text program, like notepad++, I suggest call trim() function for every line.
Looks like your file is UTF-16. These two characters are the Byte order mark of UTF-16.
You must specify that when constructing your Scanner.
final Scanner scanner = new Scanner(file, "UTF-16");
If you don't have Notepad++ (text editor) download it. Open your generated text file using it.
Do find/Replace and populate the fields and check the settings by looking at the image below. then press Replace All. And then save your file. Your text file will be clean.

File Delete and Rename in Java

I have the following Java code which will search in an xml for a specific tag and then will add some text to it and save that file. I couldnt find a way to rename the emporary file to the original file. Please suggest.
import java.io.*;
class ModifyXML {
public void readMyFile(String inputLine) throws Exception
{
String record = "";
File outFile = new File("tempFile.tmp");
FileInputStream fis = new FileInputStream("InfectiousDisease.xml");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
FileOutputStream fos = new FileOutputStream(outFile);
PrintWriter out = new PrintWriter(fos);
while ( (record=br.readLine()) != null )
{
if(record.endsWith("<add-info>"))
{
out.println(" "+"<add-info>");
out.println(" "+inputLine);
}
else
{
out.println(record);
}
}
out.flush();
out.close();
br.close();
//Also we need to delete the original file
//outFile.renameTo(InfectiousDisease.xml);//Not working
}
public static void main (String[] args) {
try
{
ModifyXML f = new ModifyXML();
f.readMyFile("This is infectious disease data");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Thanks
First delete the original file and then rename the new file:
File inputFile = new File("InfectiousDisease.xml");
File outFile = new File("tempFile.tmp");
if(inputFile.delete()){
outFile.renameTo(inputFile);
}
A good method to rename files is.
File file = new File("path-here");
file.renameTo(new File("new path here"));
In your code there are several issues.
First your description mentions renameing the original file and adding some text to it. Your code doesn't do that, it opens two files, one for reading and one for writing (with the additional text). That is the right way to do things, as adding text in-place is not really feasible using the techniques you are using.
The second issue is that you are opening a temporary file. Temporary files remove themselves upon closing, so all the work you did adding your text disappears as soon as you close the file.
The third issue is that you are modifying XML files as plain text. This sometimes works as XML files are a subset of plain text files, but there is no indication that you attempted to ensure that the output file was an XML file. Perhaps you know more about your input files than is mentioned, but if you want this to work correctly for 100% of the input cases, you probably want to create a SAX writer that writes out all a SAX reader reads, with the additional information in the correct tag location.
You can use
outFile.renameTo(new File(newFileName));
You have to ensure these files are not open at the time.

Categories

Resources