« first day (3 days earlier)      last day (17 days later) » 

01:41
@Android_K.Doe
01:59
Yes?
OH HEY
oops caps
anyway was wondering is there any way to read from textfile like reading a line AFTER a specific line
for example
reading "HELLO"
but the line after "HELLO"
how can I go about that
do something like my function then split it to "Hello"
you will get 2 size array by that
use array[1]
hmm I dont get it
like I dont need to read "HELLO" but I need the line after that
so for example
"HELLO"
"abc"

I need to read that abc, and I cannot read by line number because it is not the same everytime and the abc is not going to be abc all the time so only way I thought was if its possible to read "abc" by reading the line before "abc"
02:27
yeah when you split to hello you will get the two
sample
"hi hello here"
when you split that to hello
you will get array[0] = hi array[1] = here
no no no thats not what i want
wait
This is how I read a line from a text file
File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = "--FULLY CAPTURED--";
String CurrentLine;
while ((CurrentLine = inputStream.readLine()) != null) {
if (CurrentLine.equals(lineToRead)) {
String filen = f.getName().substring(0, f.getName().lastIndexOf("."));
items.add(filen);

}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I want to read a line after "--FULLY CAPTURED--";
how can I do that
exactly just what i said
ok then can you do an example for this code
like a line after "--FULLY CAPTURED--";
array[1] from split = all text after the split word
I dont get it how can I do that
02:32
just follow the function i made before.. then instead of replace do the split
try {
BufferedReader file = new BufferedReader(new FileReader(path + "/" + filename+".txt"));
String line;
StringBuffer inputBuffer = new StringBuffer();

while ((line = file.readLine()) != null) {
inputBuffer.append(line);
inputBuffer.append('\n');
}
String inputStr = inputBuffer.toString();

file.close();


inputStr = inputStr.split("--PENDING SUBMIT--", " ");

FileOutputStream fileOut = new FileOutputStream(path + "/" + filename+".txt");
fileOut.write(inputStr.getBytes());
fileOut.close();

} catch (Exception e) {
like this?
02:57
inputStr = inputStr.split("Hello")[1]; i think this will work
ok I will give it a try
Ok I have another issue is that I have use file reader to read for multiple files like this :

public void testProfile() {
File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = "--FULLY CAPTURED--";
String CurrentLine;
while ((CurrentLine = inputStream.readLine()) != null) {
if (CurrentLine.equals(lineToRead)) {
String filen = f.getName().substring(0, f.getName().lastIndexOf("."));
Like I have 2 items on listview
ABC
DEF

if the user click on ABC then it should read ABC and look for the line to split only from the ABC
how can I do that
 
2 hours later…
05:08
@Android_K.Doe
inputStr = inputStr.split("Hello")[1];
is to read the line after hello what about reading 2 lines before hello?
05:28
0
Q: Java Android Reading A Textfile After A Specific Line Until Before A Specific Line

Kelvin JigSawingSo heres an example of my Textfile Line xx: "Start of ABC" Line xx: "Stuff I want to read" ... Line xx: "Stuff I still want to read" Line xx: (Blank) Line xx: "End of ABC" Here's the catch; the line numbers for each textfiles are not consistent, so I cannot depend on that. Neither are the "Stu...

05:39
This is very simple. Try modifying this to satisfy your need. Clue, line is a string

while ((line = file.readLine()) != null) {
inputBuffer.append(line);
inputBuffer.append('\n');
}
meaning?
but this is append of codes I need to read from line a till line b
and store that into a string
this is so easy come on.. For example

while ((line = file.readLine()) != null) {
inputBuffer.append(line);
inputBuffer.append('\n');

if(line.contains("My Word")) break;
}

String inputStr = inputBuffer.toString();

now print inputStr you will see all lines from above the line you specified
if(line.contains("My Word")) break;
the break meaning it stop reading?
break; mean to stop there.. the loop would stop
05:54
ohhh i thought break is only for switch caswe
case*
ok let me try
put the IF above so it wont include the line with the word
above inputBuffer
wait wait so this is only for the ending right so the starting reading is still using the earlier one you send me right

this one

inputStr = inputStr.split("Hello")[1];

to make it stop is
if(line.contains("My Word")) break; ?
before it appends
05:58
and another problem is it have to satisfy this condition before reading File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = filename;
String CurrentLine;
while ((CurrentLine = inputStream.readLine()) != null) {
this filename have to be read first before doing the reading on specific lines
06:15
@Android_K.Doe
help me check my codef
public void testOn()
{
SharedPreferences sharedTest = getSharedPreferences("MySharedTest", Context.MODE_PRIVATE);
String filename = sharedTest.getString("filename", " ");
Log.d("test", filename);


File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = filename;
String CurrentLine;
while ((CurrentLine = inputStream.readLine()) != null) {
if (CurrentLine.equals(lineToRead)) {
I implemented testOn onto a button
it crash and the error on this line String inputStr = CurrentLine.toString();
06:37
naturally it would.. check your logic, currentLine = null in the end..
null.toString() error
meaning?
then what should I put for the inputStr?
07:07
@Android_K.Doe
my new code but got error for arrayoutofbound
public void testOn()
{
SharedPreferences sharedTest = getSharedPreferences("MySharedTest", Context.MODE_PRIVATE);
String filename = sharedTest.getString("filename", " ");
Log.d("test", filename);


File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = filename;
String CurrentLine;
while ((CurrentLine = inputStream.readLine()) != null) {
if (CurrentLine.equals(lineToRead)) {
for this line inputStr = inputStr.split("--POWER OFF--")[1];
07:21
this doesnt work as well :(
public void testOn()
{
SharedPreferences sharedTest = getSharedPreferences("MySharedTest", Context.MODE_PRIVATE);
String filename = sharedTest.getString("filename", " ");
Log.d("test", filename);


File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = filename;
String CurrentLine;
while ((CurrentLine = inputStream.readLine()) != null) {
if (CurrentLine.equals(lineToRead)) {
@Android_K.Doe
This is not working too :(
public void testOn()
{
SharedPreferences sharedTest = getSharedPreferences("MySharedTest", Context.MODE_PRIVATE);
String filename = sharedTest.getString("filename", " ");
Log.d("test", filename);


File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = filename;
String CurrentLine;

int lineNoStart = 0;
int lineNoEnd = 0;
int lineCount = 0;
07:52
To get all lines before and after

boolean isAfter = false;

while ((line = file.readLine()) != null) {

if(line.contains("My Word")) isAfter = true;
else{
if(isAfter){
afterBuffer.append(line);
afterBuffer.append('\n');
}else{
beforeBuffer.append(line);
beforeBuffer.append('\n');
}
}
}
this is so easy i shoudnt be spoon feeding this
no the thing is i have to do a reading of another line before i do this
So it seems to be not working on multiple tries
public void testOn()
{
SharedPreferences sharedTest = getSharedPreferences("MySharedTest", Context.MODE_PRIVATE);
String filename = sharedTest.getString("filename", " ");
Log.d("test", filename);


File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = filename;
String CurrentLine;
while ((CurrentLine = inputStream.readLine()) != null) {
if (CurrentLine.equals(lineToRead)) {
Look at this code under the //CODE HERE I need to put yourcode tehre
but none of it works
and what is afterBuffer and beforeBuffer where do I initialize them?
If it is just a simple reading of line I wouldnt have problem thing is I need to read a line AND THEN read a specific line until a specific line and I cannot use line numbers or exact string for those lines
Check the function i made before.. Understand it.. Then here's a snippet to get lines in a textfile before and after a specific line. I wont be giving the full code because this is so easy

boolean isAfter = false;

while ((line = file.readLine()) != null) {

if(line.contains("My Word")) isAfter = true;
else{
if(isAfter){
afterBuffer.append(line);
afterBuffer.append('\n');
}else{
beforeBuffer.append(line);
beforeBuffer.append('\n');
}
}
}
I dont think you understand what I am trying to do you keep giving me codes that dont read what I need to read
And you keepappending lines to the file
I need to read it
SharedPreferences sharedTest = getSharedPreferences("MySharedTest", Context.MODE_PRIVATE);
String filename = sharedTest.getString("filename", " ");

this filename I have to read this filename first before reading for specific lines in that file

« first day (3 days earlier)      last day (17 days later) »