java 读取txt配置文件_Java 读取TXT文件和写入TxT文件

1.[图片] 1.png

17231516_KcuJ.png

2.[文件] IoReadWrite.java ~ 2KB     下载(34)

package net.dreak.io1;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.util.Scanner;

/**

* IO 读写操作

*

* @version 1.0 2014年12月17日 下午9:52:32

* @author asher-xu

* @date 1.0 2014年12月17日 下午9:52:32

*/

public class IoReadWrite {

/**

* 读取文件

*

* @param readPath

* 文件路径

* @return

*/

public String read(String readPath) {

String readTxt = "";

try {

File f = new File(readPath);

if (f.isFile() && f.exists()) {

InputStreamReader read = new InputStreamReader(

new FileInputStream(f), "UTF-8");

BufferedReader reader = new BufferedReader(read);

String line;

while ((line = reader.readLine()) != null) {

readTxt += line + "\r\n";

}

read.close();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return readTxt;

}

/**

* 写入文件

*

* @param writePath

* 文件路径

*/

public void write(String writePath, String writeTxt) {

try {

File f = new File(writePath);

if (!f.exists()) {

f.createNewFile();

}

OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");

BufferedWriter writer = new BufferedWriter(write);

writer.write(writeTxt);

writer.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

IoReadWrite readWrite = new IoReadWrite();

String iptMsg = "";

Scanner sc = new Scanner(System.in);

System.err.println("请输入内容:");

iptMsg = sc.next();

System.err.println("接收到内容:" + iptMsg);

System.out.println("----开始写文件----");

String writePath = "G:/3.txt";

readWrite.write(writePath, iptMsg);

System.out.println("----结束写文件----");

System.out.println("----读取文件开始----");

String readPath = "G:/3.txt";

String txt = readWrite.read(readPath);

System.out.println("----读取文件结束----");

System.out.println("----开始写文件----");

String writePathString = "G:/4.txt";

readWrite.write(writePathString, txt);

System.out.println("----结束写文件----");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值