Quoted Printable - Decode .eml - java

i want decode some content of an .eml-Mail-File. The File contains Strings like "Gesch=C3=A4ftsbedingungen", it shoult be > "Geschäftsbedingungen"
reader = new InputStreamReader(new FileInputStream(path));
BufferedReader in = new BufferedReader(reader);
while ((line = in.readLine()) != null)
{
System.out.println(line);
}
Im not sure how i do that. I try to use "MimeUtility", but i do not get along so.
MimeUtility test;
System.out.println(test.decode(line));

Note : MimeUtility is an utility class so it contains all static methods so you should use
System.out.println(MimeUtility.decode(line));
Though I think you should use MimeUtility.decodeText or MimeUtility.decodeWord method or commons MimeUtility.decodeText method.

Related

How to store different parts of a text file into different variables?

I am trying to read parts of a text file with the format
John Smith
72
160
The first line being the name (string), and the second and third lines being height and weight (both ints). However, I cannot find a way to store each of these into their own variables, instead I can only figure out how to store the whole thing into one variable and print it. This is the code that I have as of now
try
{
File file = new File("person.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null)
{
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println(stringBuffer);
}
In this part
stringBuffer.append(line);
stringBuffer.append("\n");
I was thinking of trying to add a part in the middle of both those lines that stored a variable, but it did not seem possible. I also thought of using a for loop and using that to my advantage somehow, but could not figure out a way to do it with that either.
Is there any possible way to do this that I do not know about? Thank you
Reading and parsing a text file in Java has been getting easier in every new version. You can try the following way:
List<String> lines = Files.lines(Paths.get("person.txt")).collect(Collectors.toList());
String name = lines.get(0);
Integer height = Integer.parseInt(lines.get(1));
Integer weight = Integer.parseInt(lines.get(2));
File file = new File("person.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String firstline = bufferedReader.readLine();
String secondline = bufferedReader.readLine();
String thirdline = bufferedReader.readLine();
fileReader.close();

BufferedReader does not work well

Loading following URL with BufferedReader, but content is not delivered. Even though a plain browser can show content. So str will remain nil. Any idea why?
URL url = new URL("http://www.omdbapi.com/?t=zorr&y=&plot=short&r=json");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {}
Log.d("alma", str);
You are ignoring all of the lines that you are reading. You then exit the loop when str becomes null. So, your Log.d() call will always show null.
If you want to use the lines that you are reading, use str inside` your currently empty block:
while ((str = in.readLine()) != null) {
// do something with str
}
You might also wish to consider using a third-party library that offers a simpler API. OkHttp3, for example, makes getting a string response from a URL fairly easy.
try this:
URL url = new URL("http://www.omdbapi.com/?t=zorr&y=&plot=short&r=json");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
Log.d("alma", str); // this should be here
}

How to reset BufferedReader (or InputStreamReader) to use readline() twice?

I use this code snippet to read text from a webpage aand save it to a string?
I would like the readline() function to start from the beggining. So it would read content of the webpage again. How Can I do that
if (response == httpURLConnection.HTTP_OK) {
in = httpURLConnection.getInputStream();
isr = new InputStreamReader(in);
br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
fullText += line;
}
// I want to go through a webpage source again, but
// I can't because br.readLine() = null. How can I put
// put a marker on the beginning of the page?
while ((line1 = br.readLine()) != null) {
fullText1 += line1;
// It will not go into this loop
}
You can only mark a position for a Reader (and return to it with reset()) if markSupported returns true, and I very much doubt that the stream returned by httpURLConnection.getInputStream() supports marks.
The best option, I think, is to read the response into a buffer and then you can create as many readers as you like over that buffer. You will need to include the line termination characters (which you are currently discarding) to preserve the line structure. (Alternatively, you can read the response into a List<String> rather than into a single String.)
From InputStream will not reset to beginning
your stream inside a BufferedInputStream object like:
with the markSupported() method if your InputStream actually support using mark. According to the API the InputStream class doesn't, but the java.io.BufferedInputStream class does. Maybe you should embed your stream inside a BufferedInputStream object like:
InputStream data = new BufferedInputStream(realResponse.getEntity().getContent());
// data.markSupported() should return "true" now
data.mark(some_size);
// work with "data" now
...
data.reset();

Reading a line of a txt file in assets

I have a text file placed in assets and I want to read one line of it at a time. My problem is that I do not know how to access the file in Activity, and then once I access it, how would I go about only selecting one line?
If keeping the txt file in assets is a bad idea, where should I put it for easier access?
I really appreciate any help!
This is a snippet I use to prepopulate tables in my RSS feed reader. You can use it as a track for your needs.
In res/raw/ I have file feeds.txt. The file is referenced is code like R.raw.feeds.
final Resources resources = mHelperContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.feeds);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream), 8192);
try {
String line;
while ((line = reader.readLine()) != null) {
//make the use you want with "line"
}
} catch (IOException e) {
Log.e(TAG, "Error loading sample feeds.");
throw new RuntimeException(e);
}
To open assests, you'll need to call
<context>.getAssets().open(<your file>);
<context> is your activity, so if this is in your onCreate, then it would be this. That call returns an inputstream, which you can then handle however you please.
I don't see how it would be a particularly bad idea to keep your text file there, depends on what you're using that text file for.
Try this:
Make a new method for example readMyFile().
It must looks like this:
private String readMyFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder txt = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
txt.append(line);
txt.append("\n");
}
reader.close();
return txt.toString();
Paste it to your code, and use the method (readMyFile([the file what you want to read in assets]).
Hope it helps.

can i read windows-1256 file in java?

I want to read an Arabic text file encoded in windows-1256 using Java (on the windows platform)
Any suggestions?
If your JVM supports that encoding, then yes, you can easily do that:
Reader r = new InputStreamReader(new FileInputStream(theFile), "Windows-1256");
BufferedReader buffered = new BufferedReader(r);
try {
String line;
while ((line = buffered.readLine()) != null) {
// handle each line
}
} finally {
buffered.close();
}
Something like:
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream("myfile.txt"), "windows-1256"));
Should work.
To read from a FileInputStream with another character set than the platform default, use an InputStreamReader:
http://download.oracle.com/javase/6/docs/api/java/io/InputStreamReader.html#InputStreamReader(java.io.InputStream,%20java.lang.String)

Categories

Resources