- java 读取 txt文件
- /*
- * 读取char
- */
- private String readtxt() throws IOException{
- BufferedReader br=new BufferedReader(new FileReader("d:/sql.txt"));
- String str="";
- String r=br.readLine();
- while(r!=null){
- str+=r;
- r=br.readLine();
- }
- return str;
- }
/*
* 读取char
*/
private String readtxt() throws IOException{
BufferedReader br=new BufferedReader(new FileReader("d:/sql.txt"));
String str="";
String r=br.readLine();
while(r!=null){
str+=r;
r=br.readLine();
}
return str;
}
- /*
- * 读取char
- */
- private String readtxt2() throws IOException{
- String str="";
- FileReader fr=new FileReader("d:/sql.txt");
- char[] chars=new char[1024];
- int b=0;
- while((b=fr.read(chars))!=-1){
- str+=String.valueOf(chars);
- }
- return str;
- }
/*
* 读取char
*/
private String readtxt2() throws IOException{
String str="";
FileReader fr=new FileReader("d:/sql.txt");
char[] chars=new char[1024];
int b=0;
while((b=fr.read(chars))!=-1){
str+=String.valueOf(chars);
}
return str;
}
- /*
- * 读取bytes
- */
- private Byte[] readtxt3() throws IOException{
- InputStream input=new FileInputStream("d:/sql.txt");
- byte[] b=new byte[1024];
- ArrayList<Byte> lsbytes=new ArrayList<Byte>();
- int n=0;
- while((n=input.read(b))!=-1){
- for(int i=0;i<n;i++){
- lsbytes.add(b[i]);
- }
- }
- return (Byte[])(lsbytes.toArray());
- }
/*
* 读取bytes
*/
private Byte[] readtxt3() throws IOException{
InputStream input=new FileInputStream("d:/sql.txt");
byte[] b=new byte[1024];
ArrayList<Byte> lsbytes=new ArrayList<Byte>();
int n=0;
while((n=input.read(b))!=-1){
for(int i=0;i<n;i++){
lsbytes.add(b[i]);
}
}
return (Byte[])(lsbytes.toArray());
}
2.java 写txt文件
- /*
- * 写char文件
- *
- */
- private static void write2() throws IOException{
- String abc=new String("test\n");
- FileWriter fw=new FileWriter("d:/sql.txt");
- fw.write("abc");//重新写文件
- fw.append(abc);//追加文件内容
- fw.close();
- }
/*
* 写char文件
*
*/
private static void write2() throws IOException{
String abc=new String("test\n");
FileWriter fw=new FileWriter("d:/sql.txt");
fw.write("abc");//重新写文件
fw.append(abc);//追加文件内容
fw.close();
}
- /*
- * 写char文件
- */
- private void write() throws IOException{
- String abc=new String("test");
- BufferedWriter bw=new BufferedWriter(new FileWriter("d:/sql.txt"));
- bw.write("abc");//重新写文件
- bw.append(abc);//追加文件内容
- bw.close();
- }
/*
* 写char文件
*/
private void write() throws IOException{
String abc=new String("test");
BufferedWriter bw=new BufferedWriter(new FileWriter("d:/sql.txt"));
bw.write("abc");//重新写文件
bw.append(abc);//追加文件内容
bw.close();
}
- /*
- * 写byte文件
- */
- private void write3() throws IOException{
- byte[] b=new byte[10];
- FileOutputStream fos=new FileOutputStream("d:/sql.txt");
- fos.write(b);
- fos.close();
- }
/*
* 写byte文件
*/
private void write3() throws IOException{
byte[] b=new byte[10];
FileOutputStream fos=new FileOutputStream("d:/sql.txt");
fos.write(b);
fos.close();
}
reader,writer是对char进行操作的。inputStream,outputStream都是对于byte进行操作的。如果是对于文
件进行的操作,使用前面加上file的类.如果数据是从外部流向内存的是inputStream,如果数据是从内存流向外部的
则使用outputStream.
3.java 读properties文件
java.util.Properties实现对properties文件的操作
- //使用"utf-8"可以读取到中文的内容
- inputps = new InputStreamReader(new FileInputStream(filename),"utf-8");
- p = new Properties();
- p.load(inputps);
- inputps.close();
//使用"utf-8"可以读取到中文的内容
inputps = new InputStreamReader(new FileInputStream(filename),"utf-8");
p = new Properties();
p.load(inputps);
inputps.close();
properties中如果包含中文,需要进行编码的转化,可以在文件写好后,使用jdk中native2ascii.exe的工具完成转换,则文件将使用utf-8进行编码。
properties文件中都是使用key/value对的形式出现的。
- /**
- * 得到itemName的对应的值
- * @param itemName 字段名
- * @return
- */
- public String getValue(String itemName) {
- return p.getProperty(itemName);
- }
/**
* 得到itemName的对应的值
* @param itemName 字段名
* @return
*/
public String getValue(String itemName) {
return p.getProperty(itemName);
}
4.java 写properties文件
- /**
- * 设置KEY值
- * @param itemName
- * @param value
- */
- public void setValue(String itemName, String value) {
- p.setProperty(itemName, value);
- }
/**
* 设置KEY值
* @param itemName
* @param value
*/
public void setValue(String itemName, String value) {
p.setProperty(itemName, value);
}
- /**
- * 保存文件
- * @param filename
- * @param description
- * @throws Exception
- */
- public void saveFile(String filename, String description) throws Exception {
- try {
- output= new OutputStreamWriter(new FileOutputStream(filename),"utf-8");
- p.store(output, description);
- output.close();
- } catch (IOException ex) {
- warning(ex.toString());
- throw new Exception("无法保存指定的配置文件:" + filename);
- }
- }
/**
* 保存文件
* @param filename
* @param description
* @throws Exception
*/
public void saveFile(String filename, String description) throws Exception {
try {
output= new OutputStreamWriter(new FileOutputStream(filename),"utf-8");
p.store(output, description);
output.close();
} catch (IOException ex) {
warning(ex.toString());
throw new Exception("无法保存指定的配置文件:" + filename);
}
}