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();
}
}
}
展开全部
/**
* 从文本中 按行读取数据。
*/
public static void B()
{
Scanner sc = null;
try
{
sc = new Scanner(new File("D:/myeclipse.txt"));
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// 按行读取数据
while (sc.hasNextLine())
{
System.out.println(sc.nextLine());
}
}
* 从文本中 按行读取数据。
*/
public static void B()
{
Scanner sc = null;
try
{
sc = new Scanner(new File("D:/myeclipse.txt"));
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// 按行读取数据
while (sc.hasNextLine())
{
System.out.println(sc.nextLine());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1楼正解,无非两个功能,一个是读取文件,一个是解析文件流
追问
你知道怎样读取一个词语吗,就是后面有空格的那种。还有怎样修改txt中指定位置的数据。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Test {
public String read(String fileName, String encoding) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), encoding));
StringBuilder result = new StringBuilder();
String tmp = null;
while((tmp = reader.readLine()) != null) {
result.append(tmp);
}
reader.close();
return result.toString();
}
public String read(String fileName) throws IOException {
return read(fileName, "GBK");
}
public static void main(String[] args) {
Test t = new Test();
try {
String result = t.read("xxxx");
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String read(String fileName, String encoding) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), encoding));
StringBuilder result = new StringBuilder();
String tmp = null;
while((tmp = reader.readLine()) != null) {
result.append(tmp);
}
reader.close();
return result.toString();
}
public String read(String fileName) throws IOException {
return read(fileName, "GBK");
}
public static void main(String[] args) {
Test t = new Test();
try {
String result = t.read("xxxx");
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public String read(String path) throws Exception { //读
File f = new File(path);
FileInputStream input = new FileInputStream(f);
BufferedInputStream buf=new BufferedInputStream(input);
byte[] b=new byte[(int) f.length()];
input.read(b);
input.close();
return new String(b);
}
public static void writeFileByByte(String path,String strs,boolean a) throws Exception{ //写
File f1=new File(path);
FileOutputStream out=new FileOutputStream(f1,a);
byte[] b=strs.getBytes();
out.write(b);
out.close();
}
File f = new File(path);
FileInputStream input = new FileInputStream(f);
BufferedInputStream buf=new BufferedInputStream(input);
byte[] b=new byte[(int) f.length()];
input.read(b);
input.close();
return new String(b);
}
public static void writeFileByByte(String path,String strs,boolean a) throws Exception{ //写
File f1=new File(path);
FileOutputStream out=new FileOutputStream(f1,a);
byte[] b=strs.getBytes();
out.write(b);
out.close();
}
追问
如果我要修改txt文本里的数据而不是单纯写入应该怎么做呢?
追答
这个有点难,光是在指定文字出添加就比较麻烦,在io中没有这个函数,只有自己写方法
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询