简体   繁体   English

在日志文件上读写

[英]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. 我正在处理我的应用程序,我自己设计了该算法,当您选择单选按钮1(例如)或类似按钮时,该算法将在日志文件中写入默认编号,而当用户离开时应用程序,然后重新打开它,算法将读取日志文件中的编号,然后通过日志文件上的默认编号启动活动。

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? 现在回到我的问题,我当前的问题是关于算法的,因为我们在Log文件中有很多数字,而算法无法获得最后一个数字,我该如何解决呢?

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. 注意:当用户打开Main_Page_FA.class ,算法在日志文件中写入编号143;当用户打开Main_Page.class ,算法在日志文件中写入编号123。

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. 使用java.io.RandomAccessFile,可以使用它查找文件中的确切位置(在本例中是文件的末尾)并读取最后一个数字。 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). 因为您将数字存储为字符串,所以必须先读取最后100个字节,然后以编程方式找到最后一个数字(通过查找最后一个逗号)。

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) {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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