I need to download a .txt file from a website, the problem is the downloaded file doesn't respect the same line wrapping as the original file.
File:
Word1
Word2
Word3
File downloaded:
Word1Word2Word3
I use this method to download (this isn't mine) :
#Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream( MegaMethods.FolderPath+"downloadedfile.txt");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
Try using a BufferedReader to read it in something like
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
StringBuilder responseData = new StringBuilder();
while((line = in.readLine()) != null) {
responseData.append(line);
}
then output the lines as necessary. I'm no where near a station where I can test this so you might have to do some fiddling.
Related
i need a solution for reading a text file which was stored in internal storage.
i don't want to read it line by line. without use looping how to read a complete text file and store it into a string.
BufferedReader br;
String line;
String data = "";
// String text="";
try {
br = new BufferedReader(new FileReader(new File(Environment.getExternalStorageDirectory(), "queue_mgr.txt")));
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
}
You can use a large byte buffer and gain some efficiency:
try
{
InputStream in = new FileInputStream (from);
OutputStream out = new FileOutputStream (to);
// Transfer bytes from in to out
byte[] buf = new byte[1024 * 10]; // 5MB would be about 500 iterations
int len;
while ((len = in.read (buf)) > 0)
out.write (buf, 0, len);
in.close ();
out.close ();
}
}
catch (FileNotFoundException e)
{
...
}
catch (IOException e)
{
...
}
That's my android code which takes 30 min to copy a 3MB .log file into a .zip and gives lots of GC_FOR_ALLOC. I also tried to change buffersize from 1k to 8k
File tempFolder=new File(Environment.getExternalStorageDirectory()+LOG_FILE_DIRECTORY_TEMP);
String filePath= Environment.getExternalStorageDirectory()+LOG_FILE_DIRECTORY_TEMP+ "/";
String fileName ="";
String zipFileName="";
String date=DATE_FORMAT_TEMP.format(new Date());
fileName = Settings.LOG_FILE_PREFIX + date+"_" + IMEI +.log;
zipFileName = Settings.LOG_FILE_PREFIX + date+"_" + IMEI +.zip;
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String result="";
String line = "";
while((line = br.readLine())!=null)
result += line;
result = getHeader() + result;
fos = new FileOutputStream(file);
fos.write(result.getBytes());
File zipFile=new File(filePath+zipFileName);
iStream = new FileInputStream(file);
oStream = new FileOutputStream(zipFile);
zos = new ZipOutputStream(oStream);
ze = new ZipEntry(fileName);
zos.putNextEntry(ze);
byte[] buffer = new byte[1024];
while((length = iStream.read(buffer)) != -1)
zos.write(buffer, 0, length);
zos.flush();
oStream.flush();
In your code it looks like you are opening and reading the file twice (your BufferedReader and your iStream object). Also, you are loading the entire file into memory twice before writing anything to memory. That's still only 6MB but you probably are hitting your memory stack limit - unless you use android:largeHeap="true" in your manifest.
Before you do that though, try just reading/writing each part:
public void zip(String[] _files, String zipFileName) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(zipFileName);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
dest));
byte data[] = new byte[BUFFER];
for (int i = 0; i < _files.length; i++) {
Log.v("Compress", "Adding: " + _files[i]);
FileInputStream fi = new FileInputStream(_files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Here is the reference: http://javatechig.com/android/how-to-programmatically-zip-and-unzip-file-in-android
An example would be a simple image.
I have tried so many things and it just refuses to work despite making a whole lot of sense.
What I've done so far is I'm able to grab 25 pictures and add them to
/sdcard/app name/sub/dir/filename.jpg
They all appear there according to the DDMS but they always have a filesize of 0.
I'm guessing it's probably because of my input stream?
Here's my function that handles the downloading and saving.
public void DownloadPages()
{
for (int fileC = 0; fileC < pageAmount; fileC++)
{
URL url;
String path = "/sdcard/Appname/sub/dir/";
File file = new File(path, fileC + ".jpg");
int size=0;
byte[] buffer=null;
try{
url = new URL("http://images.bluegartr.com/bucket/gallery/56ca6f9f2ef43ab7349c0e6511edb6d6.png");
InputStream in = url.openStream();
size = in.available();
buffer = new byte[size];
in.read(buffer);
in.close();
}catch(Exception e){
}
if (!new File(path).exists())
new File(path).mkdirs();
FileOutputStream out;
try{
out = new FileOutputStream(file);
out.write(buffer);
out.flush();
out.close();
}catch(Exception e){
}
}
}
It just keeps giving me 25 files in that directory but all of their file sizes are zero. I have no idea why. This is practically the same code I've used in a java program.
PS...
If you're gonna give me a solution... I've already tried code like this. It doesn't work.
try{
url = new URL(urlString);
in = new BufferedInputStream(url.openStream());
fout = new FileOutputStream(filename);
byte data[] = new byte[1024];
int count;
System.out.println("Now downloading File: " + filename.substring(0, filename.lastIndexOf(".")));
while ((count = in.read(data, 0, 1024)) != -1){
fout.write(data, 0, count);
}
}finally{
System.out.println("Download complete.");
if (in != null)
in.close();
if (fout != null)
fout.close();
}
}
Here's an image of what my directories look like
http://oi48.tinypic.com/2cpcprm.jpg
A bit change to your second option, try it as following way,
byte data[] = new byte[1024];
long total = 0;
int count;
while ( ( count = input.read(data)) != -1 )
{
total += count;
output.write( data,0,count );
}
This one is different in while statement while ((count = in.read(data, 0, 1024)) != -1)
Using Guava something like this should work:
String fileUrl = "xxx";
File file = null;
InputStream in;
FileOutputStream out;
try {
Uri url = new URI(fileUrl);
in = url.openStream();
out = new FileOutputStream(file)
ByteStreams.copy(in, out);
}
catch (IOException e) {
System.out.println(e.toString());
}
finally {
in.close();
out.flush();
out.close();
}
I want to read my data from my .txt file into a ReaderClass in android, fields are seperated by ";"
---- here is my solution of my last post:
public void cut()
{
try{
InputStream input =context.getResources().openRawResource(R.raw.textfile);
BufferedReader br = null;
br=new BufferedReader(new InputStreamReader(input,"iso-8859-1"));
String line = null;
while ((line = br.readLine()) != null) {
String[] decoupage= line.split(";");
String titre=decoupage[0];
String description=decoupage[1];
String reponse=decoupage[2];
String explication=decoupage[3];
String categorie=decoupage[4];
String etat=decoupage[5];
//test Logcat
Log.d("information ", " buffer");
Log.i("titre : ",titre);
Log.i("description : ",description);
Log.i("reponse : ",reponse);
Log.i("explication : ",explication);
Log.i("categorie : ",categorie);
Log.i("etat : ",etat);
}
in.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
System.err.println("\n File not found");
}
//end
}
FileInputStream fis;
fis = openFileInput("sample.txt");
StringBuffer Content = new StringBuffer("");
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
Content.append(new String(buffer));
}
you will get entire content in a string buffer ,convert it into string, then you can apply yourString.split(";") to get all values which you can keep in some array.
I have a webapplication which allows to upload binary files. I have to parse them and save the content 1:1 into a String and then into the database.
When I use uuencode on a unix machine to encode the binary file, then it works. Is there a way to do this automatically in java?
if (isMultipart) {
//Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
//Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (!item.isFormField()) {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line;
licenseString = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
// Generate License File
licenseString += line + "\n";
}
}
}
session.setAttribute("licenseFile", licenseString);
System.out.println("adding licensestring to session. ");
}
It works of course for all non-binary files uploaded. How can I extend it to support binary files?
// save to file
// =======================================
InputStream is = new BufferedInputStream(item.openStream());
BufferedOutputStream output = null;
try {
output = new BufferedOutputStream(new FileOutputStream("temp.txt", false));
int data = -1;
while ((data = is.read()) != -1) {
output.write(data);
}
} finally {
is.close();
output.close();
}
// read content of file
// =======================================
System.out.println("content of file:");
try {
FileInputStream fstream = new FileInputStream("temp.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
licenseString = "";
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println(javax.xml.bind.DatatypeConverter.printBase64Binary(strLine.getBytes()));
licenseString += javax.xml.bind.DatatypeConverter.printBase64Binary(strLine.getBytes()) + "\n";
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
You could use the commons_fileupload lib (check it here : org.apache.commons.fileupload.disk.DiskFileItem is not created properly?)
The doc is here : http://commons.apache.org/fileupload/using.html
Your case is pretty well explained on the official website.
A Better way would be to write the upload to a temporary file and then handle it from there:
if (!item.isFormField()) {
InputStream stream = new BufferedInputStream(item.getInputStream());
BufferedOutputStream output = null;
try {
output = new BufferedOutputStream(new FileOutputStream(your_temp_file, false));
int data = -1;
while ((data = input.read()) != -1) {
output.write(data);
}
} finally {
input.close();
output.close();
}
}
now you have a temporary file, which is the same as the uploaded file, you can do your 'other' calculations from there.