281~name~location~#time#room%
#time2#room2%
#time3#room3;
I need to delete the second line after the % in text file. But i dont know how to do that.
BufferedReader in = null;
out = null;
File sdCard = Environment.getExternalStorageDirectory();
File root = new File (sdCard.getAbsolutePath() + "/yourFile");
try {
InputStream instream = new FileInputStream(file);
in = new BufferedReader(new InputStreamReader(instream));
out = new PrintWriter(new File(root,"yournewFile));
String line; //a line in the file
while ((line = in.readLine()) != null) {
if (!Pattern.matches("^some pattern.*",line)) { //find the line we want to delete
//if it is not the line you want to delete then write it to new file
out.println(line);
}
}
in.close();
out.flush();
out.close();
File oldFile = new File (root,"/yourFile");
oldFile.delete();
File newFile = new File (root,"/yournewFile");
newFile.renameTo(oldFile);
}catch(Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
You should read old file and match it to the line you want to delete.if it is not the line you want to delete then write it to another file.thus you will get the new file with only that line missing which you wanted to delete.
you are done.hope it will help.
Related
I am trying to create an android app in which it will take the contents of the file which is in assets, copy the contents to the file in filestorage. Then while displaying, it will read the file in filestorage and will append the lines in edittext.
The problem i am facing is empty lines are not being read.
Following is the code snippet in which i am reading the contents of asset file and copying it in file "current.txt" which will be stored in filestorage.
reader = new BufferedReader(new InputStreamReader(getAssets().open("pravachan.txt")));
ContextWrapper contextWrapper=new ContextWrapper(getApplicationContext());
File directory= contextWrapper.getDir("FileStorage",Context.MODE_PRIVATE);
File myInternalFile=new File(directory,"current.txt");
FileWriter filewriter = new FileWriter(myInternalFile);
BufferedWriter out = new BufferedWriter(filewriter);
String mLine = reader.readLine();
while (mLine != null)
{
//process line
if(mLine.isEmpty())
{
out.write(" ");
String str = System.getProperty("line.separator");
out.write(str);
}
else
out.write(mLine);
mLine = reader.readLine();
}
out.close();
Following is the code snippet in which i am appending the edittext with contents of "current.txt" file
ContextWrapper contextWrapper=new ContextWrapper(getApplicationContext());
File directory= contextWrapper.getDir("FileStorage", Context.MODE_PRIVATE);
File myInternalFile=new File(directory,"current.txt");
FileInputStream fis=new FileInputStream(myInternalFile);
BufferedReader reader=new BufferedReader(new InputStreamReader(fis));
String mLine = reader.readLine();
TextView tv = (TextView)findViewById(R.id.editText);
while(mLine != null)
{
if(mLine.isEmpty()) {
tv.append(" ");
String str = System.getProperty("line.separator");
tv.append(str);
}
else
tv.append(mLine);
mLine = reader.readLine();
}
I don't want empty lines to be skipped.I want the file as it is to be displayed in Edittext. Please Help. Thanks in advance!
I got the answer!
Instead of doing out.write(str) in which str is the line separator, I did out.newline(). It worked!
I have a textfile in /sdcard/applit/mytext.txt
I want to push it to parse cloud.Googled a lot but No profit.Please explain completely.Thanx.
private void txtPusher(File dir) throws IOException {
File outputFile;
outputFile=newFile(dir,"MyText\t"+ParseUser.getCurrentUser().getUsername()+".txt");
byte [] b;
b=FileUtils.readFileToByteArray(outputFile);
file=new ParseFile("MyText\t"+ParseUser.getCurrentUser().getUsername()+".txt",b);
file.saveInBackground();
TextPusher Tpusher=new TextPusher(file);
Tpusher.execute();
}
Here dir is the directory I am passing to txtPusher function.I want to know wether output file is that file which I am going to push or another directory or it is creating a new file.but my file is not getting pushed.If i am wrong please share the right way to push the textfile
You can read the contents of a text (.txt) file using the following:
private String readFile(String fileName) {
//Find the directory for the SD Card using the API
File sdcard = new File(Environment.getExternalStorageDirectory() + File.separator + "Inventory_Files/Version/");
// Get the text file
File file = new File(sdcard, fileName);
// Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
//You'll need to add proper error handling here
}
return text.toString();
}
As for sending this data to the cloud, this looks to be very well documented on their site.
i am working on a project which needs to view pdf. i pretty much find it easy to view text. Is there a way i can change the below code to view pdf.
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard, textRead + ".txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.textView1);
//Set the text
tv.setText(text);;
}
Nope, it's not that easy. You'll need a library which'll let you decode the PDF File and then display. You can use one of these libraries to make things easy :
https://github.com/JoanZapata/android-pdfview
https://github.com/jblough/Android-Pdf-Viewer-Library
Try this:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Add Instructions to the user to install a PDF reader here, or something you want
}
This is my code:
File root = Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/Bonbon info");
dir.mkdirs();
File f = new File(dir, "paket.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
br.skip(60);
int charactersRead = 0;
while ((line = br.readLine()) != null && charactersRead < 12) {
text.append(line);
text.append('\n');
charactersRead++;
}
}
catch (IOException e) {
}
final String URL = text.toString();
TextView tv = (TextView)findViewById(R.id.textView2);
tv.setText(text);
Reading is working, but i can't read only 12 characters, it reads trough the end of file, don't know why.
I'm guessing your file is relatively short.
You're calling BufferedReader.readLine(), which in an attempt to be efficient is sucking up a big chunk of the file stream rather than going through it character-by-character.
If you want that finer control over what you read, it's probably worth using an InputStream implementation straight up.
I'm trying to read some text from a .txt file, here's my code:
String filePath = bundle.getString("filepath");
StringBuilder st = new StringBuilder();
try {
File sd = Environment.getExternalStorageDirectory();
File f = new File(sd, filePath);
FileInputStream fileis = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(
fileis));
String line = new String();
while ((line = buf.readLine()) != null) {
st.append(line);
st.append('\n');
}
Log.i("egor", "reading finished, line is " + line);
} catch (FileNotFoundException e) {
Log.i("egor", "file not found");
} catch (IOException e) {
Log.i("egor", "io exception");
}
reader.setText(st.toString());
The text looks like this:
This is a sample text to test
The .txt file is created in Windows notepad.
And here's what I'm getting:
What's wrong with my code? Thanks in advance.
Is the file in utf-8 (unicode) format? For some reason, Notepad always adds a byte-order mark to unicode files, even when the byte-order is irrelevant. When interpreted as ASCII or ANSI, the BOM will be seen as several characters. It's possible this is what's causing your problem.
If so, the solution is to use a more competent text editor than Notepad, or write code that checks for a BOM first in all unicode files.
If none of this makes sense to you, try googling 'unicode' and 'byte-order mark'.
Wrap a FileReader object in the BufferedReader object instead.
http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileReader.html
File sd = Environment.getExternalStorageDirectory();
File file = new File(sd, filePath);
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
st.append(line);
st.append("\n");
}
br.close();
Try with the folowing code
File f = new File(str);
FileInputStream fis = new FileInputStream(f);
byte[] mydata1 = new byte[(int) f.length()];
fis.read(mydata1);
System.out.println("...data present in 11file..."+new String(mydata1));