当前位置: 技术问答>java相关
java读取文本文件的问题! 今天下午6点准时结帖!!!
来源: 互联网 发布时间:2015-01-24
本文导语: 如何读取以下两种格式中需要的文件内容? 1.Table (Comma-delimited ASCII)文本文件: 例:sample.txt "Block","Member" "Root","B001" "B001","A001" 这其实是用逗号分隔的表,比如我想读root对应的值b001。 2.sample.ini [Course] Course_Cre...
如何读取以下两种格式中需要的文件内容?
1.Table (Comma-delimited ASCII)文本文件:
例:sample.txt
"Block","Member"
"Root","B001"
"B001","A001"
这其实是用逗号分隔的表,比如我想读root对应的值b001。
2.sample.ini
[Course]
Course_Creator = Jrace
Course_ID = c001
Course_System = FRONTPAGE2000
Course_Title = THINKING IN JAVA
Level = 1
Max_Fields_CST = 2
Total_AUs = 3
Total_Blocks = 0
Version = 3.0
[Course_Behavior]
Max_Normal = 0
[Course_Description]
The the single AU (assignable unit) in this course is the AICC Sample lesson
这是一个windows的ini格式文件,比如我想得到core组中Course_Creator主键的值!
最好给我相应或类似的源码,先谢谢了!
1.Table (Comma-delimited ASCII)文本文件:
例:sample.txt
"Block","Member"
"Root","B001"
"B001","A001"
这其实是用逗号分隔的表,比如我想读root对应的值b001。
2.sample.ini
[Course]
Course_Creator = Jrace
Course_ID = c001
Course_System = FRONTPAGE2000
Course_Title = THINKING IN JAVA
Level = 1
Max_Fields_CST = 2
Total_AUs = 3
Total_Blocks = 0
Version = 3.0
[Course_Behavior]
Max_Normal = 0
[Course_Description]
The the single AU (assignable unit) in this course is the AICC Sample lesson
这是一个windows的ini格式文件,比如我想得到core组中Course_Creator主键的值!
最好给我相应或类似的源码,先谢谢了!
|
我有一个ini文件的读取,是照以前做的一个C语言改过来的,所以风格像C语言,不过还是挺好用,代码如下:
test_getini.java文件:
import java.io.*;
public class test_getini
{
public static void main(String args[])
{
String s_rtn1="";
String value1="";
String s_section="file_path";
String s_key="client";
String s_section_list1[]=new String[100];
int i=0,i_count=0;
c_get_ini getit=new c_get_ini();
s_rtn1=getit.get_ini("config.ini",s_section,s_key,2);
System.out.println("return value in main is: {"+s_rtn1+"}");
i_count=getit.get_key_count("config.ini",s_section,s_key);
System.out.println("key count: "+i_count);
i_count=getit.get_section_list("config.ini",s_section_list1);
System.out.println("section count: "+i_count);
for(i=0;i0))
return i_sameKeyCount;
if((s_fileContent[i].startsWith("["))&&(s_fileContent[i].endsWith("]")))
return 0;
i_t1=s_fileContent[i].indexOf("=");
if(!(i_t1>0))
continue;
s_key_temp=s_fileContent[i].substring(0,i_t1).trim();
//System.out.println("current line key is : "+s_key_temp);
if(!(s_key_temp.equals(s_key)))
{
//System.out.println("unequal--> s_key_temp:'"+s_key_temp+"'t"+"s_key:'"+s_key+"'");
continue;
}
i_sameKeyCount++;
} //end while(true);
} //end for
return 0;
}
catch(Exception e)
{
return 0;
}
} //end get_key_count()
//返回指定配置文件的小节的个数和内容
public int get_section_list(String s_fileName,String s_section_list[])
{
int i_count_section=0;
//---------------
int i=0;
int i_t1=0,i_t2=0;
String s_line="",s_section_temp="";
try
{
//read file
file_string fs1=new file_string();
i_fileLineCount=fs1.file2string(s_fileName,s_fileContent);
i_count_section=0;
for(i=0;i
test_getini.java文件:
import java.io.*;
public class test_getini
{
public static void main(String args[])
{
String s_rtn1="";
String value1="";
String s_section="file_path";
String s_key="client";
String s_section_list1[]=new String[100];
int i=0,i_count=0;
c_get_ini getit=new c_get_ini();
s_rtn1=getit.get_ini("config.ini",s_section,s_key,2);
System.out.println("return value in main is: {"+s_rtn1+"}");
i_count=getit.get_key_count("config.ini",s_section,s_key);
System.out.println("key count: "+i_count);
i_count=getit.get_section_list("config.ini",s_section_list1);
System.out.println("section count: "+i_count);
for(i=0;i0))
return i_sameKeyCount;
if((s_fileContent[i].startsWith("["))&&(s_fileContent[i].endsWith("]")))
return 0;
i_t1=s_fileContent[i].indexOf("=");
if(!(i_t1>0))
continue;
s_key_temp=s_fileContent[i].substring(0,i_t1).trim();
//System.out.println("current line key is : "+s_key_temp);
if(!(s_key_temp.equals(s_key)))
{
//System.out.println("unequal--> s_key_temp:'"+s_key_temp+"'t"+"s_key:'"+s_key+"'");
continue;
}
i_sameKeyCount++;
} //end while(true);
} //end for
return 0;
}
catch(Exception e)
{
return 0;
}
} //end get_key_count()
//返回指定配置文件的小节的个数和内容
public int get_section_list(String s_fileName,String s_section_list[])
{
int i_count_section=0;
//---------------
int i=0;
int i_t1=0,i_t2=0;
String s_line="",s_section_temp="";
try
{
//read file
file_string fs1=new file_string();
i_fileLineCount=fs1.file2string(s_fileName,s_fileContent);
i_count_section=0;
for(i=0;i