Reading and displaying large formatted text documents in Android - java

I'm developing an Android app which is supposed to present large text files (books for example), for the user to browse, read, and search.
My question is as follows:
How should I read and present the text file, which is currently in either a PDF or Word format, and is formatted?
What file should the text be in (.doc, .txt, .xml, .html)?
What controls/elements and code should I use to read it on the app so that it should be presented efficiently and formatted correctly (TextView, WebView, PDF reader, or some other way)?
Thanks.

It really depends on your application and programming skills,
Grabbing texts from PDFs, word files, etc.. can be done using
libraries (lots are available)
you can save your files as raw text files (they could be readable
manually) if you want to want to secure it you can encrypt it before
saving and you can save with custom extension( .lib,.abc etc..).
TextViews are the easiest as you can change the colors ,
fonts and text size, it's really fast and easy to deal with.
Edit : example of reading a text file
File myFile = new File("/sdcard/filename.txt");
FileInputStream iStr = new FileInputStream(myFile);
BufferedReader fileReader = new BufferedReader(new InputStreamReader(iStr ));
String TextLine= "";
String TextBuffer = "";
while ((TextLine= fileReader.readLine()) != null) {
TextBuffer += TextLine+ "\n";
}
textView1.setText(TextBuffer );
fileReader.close();
example of writing a text file :
File myFile = new File("/sdcard/filename.txt");
myFile.createNewFile();
FileOutputStream oStr = new FileOutputStream(myFile);
OutputStreamWriter fileWriter= new OutputStreamWriter(oStr);
fileWriter.append(textView1.getText());
fileWriter.close();
fOut.close();

Related

How to convert InputStream to a PDF in Java, without damaging the file?

I have an InputStream which I would like to convert to a PDF, and save that PDF in a directory. Currently, my code is able to convert the InputStream to a PDF and the PDF does show up in the correct directory. However, when I try to open it, the file is damaged.
Here is the current code:
InputStream pAdESStream = signingServiceConnector.getDirectClient().getPAdES(this.statusReader.getStatusResponse().getpAdESUrl());
byte[] buffer = new byte[pAdESStream.available()];
pAdESStream.read(buffer);
File targetFile = new File(System.getProperty("user.dir") + "targetFile2.pdf");
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
Originally, the InputStream was a pAdES-file (https://en.wikipedia.org/wiki/PAdES). However, it should be able to be read as just a regular PDF.
Does anyone know how to convert the InputStream to a PDF, without getting a damaged PDF as a result?
Hello it might be a bit late but you can use PDFBOX api (or itextpdf)
https://www.tutorialkart.com/pdfbox/create-write-text-pdf-file-using-pdfbox/
here is a tuto of the process gl

Java : reading all files

I am attempting to read in files into a java application running via netbeans. I have been successful in previewing the files, but I can only preview .txt files. How can I alter my code to read in any file(s)? (eg. .doc, .docx, .pdf, .jpg, .png).
JFileChooser share = new JFileChooser();
share.showOpenDialog(null);
File f = share.getSelectedFile();
String fileName = f.getAbsolutePath();
try {
FileReader reader = new FileReader(fileName);
BufferedReader br = new BufferedReader(reader);
jTextArea1.read(br, null);
br.close();
jTextArea1.requestFocus();
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "File not found", "Error", JOptionPane.ERROR_MESSAGE);
}
you should use something like apache tika
http://tika.apache.org/
this will allow you to read almost any kind of file
also have a look at java.io.File.list() to find out the types of files you have at a location
You can read them alright ; it's just that they are huge blobs of binary data you can't make any sense of without the appropriate tools. Open one of them with a notepad and you'll get what I'm saying.
Their associated software (Word, Reader, etc...) usually do the decoding, but you may find java libraries that can do as much.

Being able to read a .dat in English, and then being able to edit the file

File file = new File(directory + player.getUsername() + ".dat");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream outFile = new FileOutputStream(file);
DataOutputStream write = new DataOutputStream(outFile);
write.writeUTF(player.getUsername());
write.writeUTF(player.getPassword());
write.writeInt(player.getStaffRights());
write.writeInt(player.getPosition().getX());
write.writeInt(player.getPosition().getY());
write.writeInt(player.getPosition().getZ());
write.writeInt(player.getGender());
Ok so pretty much what this code above does is it makes new character files for this game im working with. But the problem im having is that the character information that this code is putting into a .dat I cant read when I try and open in lets say notepad its just gibberish. I need to be able to open these .dats and be able to read/edit the text in english. Any help?
When you save data with a DataOutputStream, it will be saved in Java's native binary serialization format, not as plain text which you can read with for example Notepad.
If you want to write plain text to a file, use one of the subclasses of java.io.Writer to write to the file instead of DataOutputStream - for example PrintWriter.
PrintWriter out = new PrintWriter(file);
out.println(player.getUsername());
// etc...
// Also, don't forget to close when you are done
out.close();
Have the player object implement Serilizable and as long as all of its properties are serilizable as well, such as strings and ints, the serialization will be done.
Refer to the serialization tutorial:
Tutorial

Java scanner reading garbage

I am reading a text file using java Scanner.
try {
while(sc.hasNextLine()) {
//Read input from file
inputLine = sc.nextLine().toUpperCase();
System.out.println(inputLine);
}
The above gives below output while my text file only includes "aabbcc".
How to avoid scanner from scanning the garbage?
Thanks.
{\RTF1\ANSI\ANSICPG1252\COCOARTF1265\COCOASUBRTF210
{\FONTTBL\F0\FSWISS\FCHARSET0 HELVETICA;}
{\COLORTBL;\RED255\GREEN255\BLUE255;}
\PAPERW11900\PAPERH16840\MARGL1440\MARGR1440\VIEWW10800\VIEWH8400\VIEWKIND0
\PARD\TX566\TX1133\TX1700\TX2267\TX2834\TX3401\TX3968\TX4535\TX5102\TX5669\TX6236\TX6803\PARDIRNATURAL
\F0\FS24 \CF0 AABBCC}
You are reading a RTF Document. If you want to read the text only you can try reading it into a byte array and parsing out the text using swings rtfeditorkit.
Path path = Paths.get("path/to/file");
byte[] data = Files.readAllBytes(path);
RTFEditorKit rtfParser = new RTFEditorKit();
Document document = rtfParser.createDefaultDocument();
rtfParser.read(new ByteArrayInputStream(data), document, 0);
String text = document.getText(0, document.getLength());
This was solved by setting TextEdit preferences, Format to "Plain text" and recreated the input file.
Managed to get the output without garbage.
Source: File input in Java for Mac
The problem isn't that the Scanner is reading in garbage. It is that your file isn't plain text. From the looks of it, your file is actually "rich text", and that garbage contains formatting info. I was able to produce similar output by saving a .rtf using MS WordPad.

Parsing a CSV File In JSP

I am creating a webApp that will download some data from Yahoo Finance into a CSV file and then (hopefully) be able to then read the created CSV data into a HTML table.
I have successfully got the program to connect to the Yahoo feed and then download that data into a CSV file and would not like to use the data from the file into a table.
Below is the code i used to create the CSV file:
String ticker = request.getParameter("stockSym");
URL url = new URL("http://finance.yahoo.com/d/quotes.csv?s=" + ticker + "&f=abc");
InputStream in = url.openStream();
BufferedInputStream bufIn = new BufferedInputStream(in);
File f=new File("stockInfo.csv");
FileOutputStream fop=new FileOutputStream(f);
for (;;)
{
int data = bufIn.read();
// Check for EOF
if (data == -1)
break;
else
fop.write((char) data);
}
fop.flush();
fop.close();
Are there any JSP programmers who would know how to open and then parse a CSV file into a table or would know of any good links to tutorials on how to accomplish this task?
Thanks.
Do not write the CSV parsing code yourself, use one of the many available libraries.
They can handle tricky things like line breaks, quotes, embedded commas and so on.
There is open source routine to parse CSV files here:
http://scm.opendap.org:8090/trac/browser/trunk/ODC/src/opendap/clients/odc/Utility.java
This utility class has a bunch a different functionality in it. Just pull out what is needed to parse the CSV.

Categories