读取逗号分隔的配置文件的最佳方法是什么? [英] What is the best way to read a comma delimited configuration file?
问题描述
foo,bar
foo2,bar3
我想将这个文件读入 HashMap
键(foo)映射到一个值(bar)。
如何做到这一点最好的方法是什么?
你可以使用x = y而不是x,y然后你可以使用Properties类。
如果你确实需要有x,y,那么看看 java.util.Scanner 可以设置使用的分隔符作为分隔符(javadoc显示了这样做的例子)。 和Properties版本简单的解析,因为没有) I have a comma delimited configuration file. The empty lines are ignored and there need to be errors on invalid lines: foo,bar foo2, bar3 I want to read this file into a What is the best way to do this? If you can use x = y instead of x, y then you can use the Properties class. If you do need to have x, y then look at the java.util.Scanner you can set the delimiter to use as a separator (the javadoc shows examples of doing that). and the Properties version (way simpler for the parsing, as there is none)
这篇关于读取逗号分隔的配置文件的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ p $ import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
$ b $ class main
{
public static void main(final String [] argv)
{
final File file;
file = new File(argv [0]);
尝试
{
最终扫描仪扫描仪;
scanner =新的扫描仪(文件);
$ b $ while(scanner.hasNextLine())
{
if(scanner.hasNext(。*,))
{
String key;
final字符串值;
key = scanner.next(。*,)。trim();
if(!(scanner.hasNext()))
{
//选择一个更好的异常来抛出
throw new错误(Missing value for key: +键);
}
key = key.substring(0,key.length() - 1);
value = scanner.next();
System.out.println(key =+ key +value =+ value);
$ b catch(final FileNotFoundException ex)
{
ex.printStackTrace();
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;
$ b $ class main
{
public static void main(final String [] argv)
{
读者读者;
reader = null;
尝试
{
final Properties properties;
reader = new BufferedReader(
new FileReader(argv [0]));
properties = new Properties();
properties.load(reader);
System.out.println(properties);
catch(final IOException ex)
{
ex.printStackTrace();
finally
if(reader!= null)
{
try
{
reader.close() ;
catch(final IOException ex)
{
ex.printStackTrace();
code $ pre
HashMap
where the key (foo) is mapped with a value (bar). import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class Main
{
public static void main(final String[] argv)
{
final File file;
file = new File(argv[0]);
try
{
final Scanner scanner;
scanner = new Scanner(file);
while(scanner.hasNextLine())
{
if(scanner.hasNext(".*,"))
{
String key;
final String value;
key = scanner.next(".*,").trim();
if(!(scanner.hasNext()))
{
// pick a better exception to throw
throw new Error("Missing value for key: " + key);
}
key = key.substring(0, key.length() - 1);
value = scanner.next();
System.out.println("key = " + key + " value = " + value);
}
}
}
catch(final FileNotFoundException ex)
{
ex.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;
class Main
{
public static void main(final String[] argv)
{
Reader reader;
reader = null;
try
{
final Properties properties;
reader = new BufferedReader(
new FileReader(argv[0]));
properties = new Properties();
properties.load(reader);
System.out.println(properties);
}
catch(final IOException ex)
{
ex.printStackTrace();
}
finally
{
if(reader != null)
{
try
{
reader.close();
}
catch(final IOException ex)
{
ex.printStackTrace();
}
}
}
}
}