简体   繁体   中英

Reading and Writing on Log file

I working on my application and I designed the algorithm in this application by myself that when you chose a radio button 1 (for example) or etc., the algorithm will be writing a default number in the log file, and when the user leaves the application and then reopen it, the algorithm will be reading the number in Log file and then start an activity by that default number on Log file.

Now back to my problem, my current problem is about the algorithm because we have a so many number in Log file and algorithm can't get the last number, how can I solve it?

Note: When the user open Main_Page_FA.class the algorithm write number 143 in log file and When the user open Main_Page.class the algorithm write number 123 in log file.

Here is my code:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
BufferedWriter MakeDir = new BufferedWriter(new FileWriter("/mnt/sdcard/test.log", true));
MakeDir.close();
} catch (IOException e) {
}

try {
BufferedReader out = new BufferedReader(new FileReader("/mnt/sdcard/test.log"));
String LogCodes = out.readLine();
String [] LogSplits = LogCodes.split(",");
int LogCharSize = LogSplits.length;
String LogResults = LogSplits[LogCharSize -1];
if (LogCharSize > 1){
if (LogResults == "123"){
startActivity(new Intent(Splash_Page.this, Main_Page.class));   
}else if (LogResults == "148"){
startActivity(new Intent(Splash_Page.this, Main_Page_FA.class));
}else if (LogCodes == null){
startActivity(new Intent(Splash_Page.this, Main_Page.class));
}
}
out.close();
} catch (IOException e) {
}

Thanks

Use java.io.RandomAccessFile using which you can seek to exact position in your file (which is the end of the file in your case) and read last number. This way you'll avoid having to read whole file in the memory which is both time and memory consuming.

Because you store your numbers as string, you'll have to read let's say last 100 bytes and find last number programatically (by looking for last comma).

OK , Well i was change my algorithm and make another algorithm that detect the file empty or not , and this is work great , Here is me code:

try {
String ExternalLocation = Environment.getExternalStorageDirectory().getAbsolutePath();
File MakeLogDir = new File(ExternalLocation + "/TESTFolder");
MakeLogDir.mkdirs();
BufferedWriter PersianLog = new BufferedWriter(new FileWriter(ExternalLocation + "/TESTFolder/test1.log",true));
BufferedWriter EnglishLog = new BufferedWriter(new FileWriter(ExternalLocation + "/TESTFolder/test2.log",true));
BufferedReader PersianLogFile = new BufferedReader(new FileReader(ExternalLocation + "/TESTFolder/test1.log"));
BufferedReader EnglishLogFile = new BufferedReader(new FileReader(ExternalLocation + "/TESTFolder/test2.log"));
int PersianLogResults = PersianLogFile.read();
int EnglishLogResults = EnglishLogFile.read();
if (PersianLogResults == -1){
startActivity(new Intent(Splash_Page.this, Main_Page.class));
}else if (EnglishLogResults == -1){
startActivity(new Intent(Splash_Page.this, Main_Page_FA.class));
}
PersianLog.close();
EnglishLog.close();
PersianLogFile.close();
EnglishLogFile.close();
} catch (IOException e) {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM