用java代码读写文件,文件的读写java-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

用java代码读写文件,文件的读写java

java读写同一个文件

读取文件有三种方式,r, w, a,读,覆盖写, 追加写, new FileReader(file)以读的方式打开了文件,两个马上就以new FileWriter(file)方式覆盖写文件,文件自然是空白的,之后你readLine读到的是null,即String line是null,然后你要writer.write(line); 即writer.write(null); 自然报空指针;

成都创新互联公司2013年开创至今,先为龙港等服务建站,龙港等地企业,进行企业商务咨询服务。为龙港企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

建议,如果边读边写,考虑RandomAccessFile类

求用java读写properties文件的代码

Java代码

package com.LY;

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import java.util.Properties;

public class TestMain {

// 根据key读取value

public static String readValue(String filePath, String key) {

Properties props = new Properties();

try {

InputStream in = new BufferedInputStream(new FileInputStream(

filePath));

props.load(in);

String value = props.getProperty(key);

System.out.println(key + value);

return value;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

// 读取properties的全部信息

public static void readProperties(String filePath) {

Properties props = new Properties();

try {

InputStream in = new BufferedInputStream(new FileInputStream(

filePath));

props.load(in);

Enumeration en = props.propertyNames();

while (en.hasMoreElements()) {

String key = (String) en.nextElement();

String Property = props.getProperty(key);

System.out.println(key + Property);

}

} catch (Exception e) {

e.printStackTrace();

}

}

// 写入properties信息

public static void writeProperties(String filePath, String parameterName,

String parameterValue) {

Properties prop = new Properties();

try {

InputStream fis = new FileInputStream(filePath);

// 从输入流中读取属性列表(键和元素对)

prop.load(fis);

// 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。

// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。

OutputStream fos = new FileOutputStream(filePath);

prop.setProperty(parameterName, parameterValue);

// 以适合使用 load 方法加载到 Properties表中的格式,

// 将此 Properties 表中的属性列表(键和元素对)写入输出流

prop.store(fos, "Update '" + parameterName+ "' value");

} catch (IOException e) {

System.err.println("Visit " + filePath + " for updating "

+ parameterName + " value error");

}

}

public static void main(String[] args) {

readValue("info.properties", "url");

writeProperties("info.properties", "age","22");

readProperties("info.properties");

System.out.println("OK");

}

}

java怎样实现读写TXT文件

主要有用到java原生态的Io类,没有第三个包。直接上代码:

import java.io.*;

public class write {

public static void main(String[] args) {

write("E://123.txt", "hello");

}

public static void write(String path, String content) {

try {

File f = new File(path);

if (f.exists()) {

System.out.println("文件存在");

} else {

System.out.println("文件不存在,正在创建...");

if (f.createNewFile()) {

System.out.println("文件创建成功!");

} else {

System.out.println("文件创建失败!");

}

}

BufferedWriter output = new BufferedWriter(new FileWriter(f));

output.write(content);

output.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Java如何读写txt文件的代码

有关Java如何读写txt文件这个问题经常在面试时会被问到,不懂或不熟悉的同志们可是要记好了哟!先来看下具体实现吧! package common; import java.io.*; import java.util.ArrayList; public class IOTest { public static void main (String args[]) { ReadDate(); WriteDate(); } /** * 读取数据 */ public static void ReadDate() { String url = “e:/2.txt”; try { FileReader read = new FileReader(new File(url)); StringBuffer sb = new StringBuffer(); char ch[] = new char[1024]; int d = read.read(ch); while(d!=-1){ String str = new String(ch,0,d); sb.append(str); d = read.read(ch); } System.out.print(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 写入数据 */ public static void WriteDate() { try{ File file = new File(“D:/abc.txt”); if (file.exists()) { file.delete(); } file.createNewFile(); BufferedWriter output = new BufferedWriter(new FileWriter(file)); ArrayList ResolveList = new ArrayList(); for (int i = 0; i 10; i++) { ResolveList.add(Math.random()* 100); } for (int i=0 ;i output.write(String.valueOf(ResolveList.get(i)) + “\n”); } output.close(); } catch (Exception ex) { System.out.println(ex); } } }


本文题目:用java代码读写文件,文件的读写java
文章位置:http://kswsj.cn/article/hsggho.html

其他资讯