java 如何解析txt文档,输入检索信息,然后输出
天蓬老师
天蓬老师 2017-04-18 09:03:32
0
3
611

请输入代码需求:

请解析config.txt,文件格式包括段落名称、字段名称和字段值。段落名称的值为:"segment:"右边的字符串,在一个文件中不会有重复的段落名称;字段的名称为"="左边的字符串,字段的值为"="右边的字符串。例如:ipconfig是段落,ip是字段名称,字段值是"192.168.0.1",文件中以#起始的行为注释行,需要跳过,不做解析。

要求:

  1. 文件名称、段落名称、字段名称需要作为最终执行程序的命令行参数输入,程序返回字段值,输出到屏幕;

  2. 文件行中可能有多余的空白(空格或者制表符),比如等号前、等号后、前导空白符等,需要过滤这些空白符,只输出字段值;

  3. 需要注意:要判断是否是注释行,如果是注释行则跳过不做处理;

  4. 需要注意:要判断文件中是否有空白行,如果有空白行则跳过不做处理;

  5. 需要注意:段落名称不可能重复,但是,不同的段落中可能存在相同的字段名称;

  6. 需要注意:如果出现非法的命令行输入参数,需要有明确并恰当的提示;
     

环境提示:

  • linux操作系统默认安装选项包括了C开发环境,命令行可以直接编译;

  • windows操作系统的C程序开发可以使用VC等IDE环境编写。

java和操作系统无关,建议安装JDK后,使用文本编辑器编辑程序(如notepad、UE、vi等),在命令提示符下直接使用javac命令编译成class文件,使用java命令执行程序。

示例(java程序):

输入:javac GetConfig.java #进行编译;
       java GetConfig config.txt jdbcconfig username #命令行执行程序
       
       #三个参数:
       1、文件名称  
       2、段落名称  
       3、字段名称
       
       #本程序实现:根据段落名称和字段名称从指定的文件中取值。
输出:zhangsan    #输出结果

这是txt文档内容

#   net.conf
#   2009/12/20
    
#ipconfig 
segment:ipconfig
ip=     192.168.0.1
port=81
timeout=600000

#idbcconfig
segment:jdbcconfig
ip   = 192.168.0.2
port=8080
username=zhangsan

password=000000

#ftpconfig
segment:ftpconfig
ip= 192.168.0.3
port=21
username=mng

password=qqhbc

#websit
segment:websit
ip=192.168.0.222
port=8000
address=  http://www.sina.com.cn

#netconfig
segment:netconfig
ip = 192.168.9.28 
port= 1045
mac= AA.EF.FF.BA.3F.7A
timeout=600000
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(3)
Peter_Zhu

Scan and analyze characters one by one, it’s not difficult.
For this kind of question, you should first try to write a program to analyze it yourself, and then ask if you encounter any problems. Don’t expect that if you post the question, others will give you the program directly.
You must have the awareness to do it yourself, have enough food and clothing, and learn programming.

小葫芦

This is not complicated. . Let's look at the reading of the file. First, read the txt, and then analyze the string.

大家讲道理

Thinking:
1. Understand the command
What does javac GetConfig.java do? javac GetConfig.java是干什么的?
java GetConfig config.txt jdbcconfig username
该命令是运行编译好的GetConfig文件,那么相应的参数config.txt jdbcconfig usernamejava GetConfig config.txt jdbcconfig username

This command is to run the compiled GetConfig file, so how do the corresponding parameters config.txt jdbcconfig username be obtained by your program? Yes, search for "java main args" by yourself

2. When you know the target file name, how to use java to read the local file into the memory and search for "java read file" by yourself

3. After the file is read into the memory, you can perform string processing according to the requirements in the question and search for "java String usage" by yourself


Tips: You can read the file line by line, and then each line can be parsed according to the requirements. When the target field name is found, the value can be output directly.

Please try more. 🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template