简体   繁体   English

使用Java中的Scanner类读取文本文件的特定部分

[英]Reading specific parts of a text file using Scanner class in Java

I have gone through most of the questions related to reading a Text file using the Scanner class here, but I still don't understand how I parse the following text into different variables and then replace that text with the changed variable values. 我已经解决了与使用Scanner类读取文本文件有关的大多数问题,但是我仍然不明白如何将以下文本解析为不同的变量,然后用更改后的变量值替换该文本。 Here is the text : 这是文本:

## UserID1.Credentials : 
[Username] = {default}
[Password] = {passwordforadmin}
[AccType] = {Admin.FirstDefault}
[AccessLevel] = {1}
##

Now, I need the above text parsed into the following variables, where : 现在,我需要将上面的文本解析为以下变量,其中:

int UserID = 1;
String Username = "default";
String Password = "passwordforadmin";
String AccType = "Admin";
String AccUnderType = "FirstDefault";
int AccessLevel = 1;

The method I am using to read the text file is : 我用来读取文本文件的方法是:

String content = new Scanner(new File("D:/EncryptedCredentials.txt")).useDelimiter("\\Z").next();

Where content is the String which reads the text file (D:/EncryptedCredentials.txt) and the delimiter '\\Z' is used. 其中content是读取文本文件(D:/EncryptedCredentials.txt)的字符串,并使用定界符'\\ Z'。 (I need to load the whole text file into a String, because it would be continuously modified by the program, and further details should be added automatically, like if the User logged in at a specific time, say 11:00 AM, and logged out at 3:00 PM, and used A,B and C features, then the above text should be modified to : (我需要将整个文本文件加载到字符串中,因为程序会不断对其进行修改,并且应自动添加更多详细信息,例如,如果用户在特定时间(例如11:00 AM)登录并登录在3:00 PM退出,并使用了A,B和C功能,则以上文字应修改为:

## UserID1.Credentials : 
[Username] = {default}
[Password] = {passwordforadmin}
[AccType] = {Admin.FirstDefault}
[AccessLevel] = {1}
[LastLogInTime] = {1100}
[LastLogOutTime] = {1500}
[TotalTimeSpent] = {0400}
[FeaturesUsed] = {A,B,C}
##

How do I go about this reading and writing to a text file ? 我该如何读取和写入文本文件? Another question is, since anyone can evidently see I am doing a login system, I need to know how encryption can be implemented into the text file so that people can access the contents of the text file only through the program. 另一个问题是,既然显然每个人都可以看到我正在使用登录系统,那么我需要知道如何在文本文件中实现加密,以便人们只能通过该程序访问文本文件的内容。 The text can be changed, of course. 当然可以更改文本。 All I need is for the program to read it and store specific information in the appropriate variables, and then to write it back to the encrypted text file. 我需要的是程序读取它并将特定信息存储在适当的变量中,然后将其写回到加密的文本文件中。

So, in all, what I need to know is : 因此,总的来说,我需要知道的是:

How to read only specific parts of a text file loaded onto a String ? 如何仅读取加载到字符串的文本文件的特定部分? How to modify those parts (including adding text that wasn't present before) and write them again to the text file ? 如何修改这些部分(包括添加以前不存在的文本)并将它们再次写入文本文件? How to do the above steps while implementing encryption into the text file ? 在对文本文件实施加密时,如何执行上述步骤?

Please explain them to me, because I don't have much knowledge about this. 请向我解释一下,因为我对此并不了解。 Thanks in advance. 提前致谢。

Note : I'm still a beginner in Java, and I have no idea of anything more than Scanner classes, Arrays, loops, etc. and some random stuff. 注意:我仍然是Java的初学者,除了Scanner类,数组,循环等以及一些随机的东西外,我什么都没有。 Please help me out. 请帮帮我。

I suggest simpler approach by using properties file. 我建议通过使用属性文件更简单的方法。 Here is a link to an example: http://www.mkyong.com/java/java-properties-file-examples/ 这是一个示例的链接: http : //www.mkyong.com/java/java-properties-file-examples/

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

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