txt文件的创建与读写
来源:互联网 发布:linux arp广播 编辑:程序博客网 时间:2024/05/01 17:52
【建文件夹下的txt文件,并且按行写数据】
public class test1 {
public static void main(String[] args) throws IOException {
String directory="D:";
String fileName="myFile.txt";
File f=new File(directory,fileName);
//判断D盘是否含有myFile.txt文件
if(f.exists()){
System.out.println(f.getAbsolutePath());
System.out.println(f.getName());
System.out.println(f.length());
//写操作
FileWriter fw = new FileWriter(f,true); //设置成true就是追加
fw.write("ee");
fw.write("\r\n");//换行写入
fw.close();
System.out.println(f.length());
}else{
//目录中没有myFile.txt的文件,先创建文件所在的目录
f.getParentFile().mkdirs();
try{
//创建新文件
f.createNewFile();
//写操作
FileWriter fw = new FileWriter(f,true); //设置成true就是追加
fw.write("ee");
fw.write("\r\n");
fw.flush();
fw.close();
System.out.println(f.length());
}catch(IOException e){
System.out.println("创建新文件时出现了错误。。。");
e.printStackTrace();
}
}
}
}
【把txt文件按行形式读出来】
public class read {
public static void main(String[] args) throws IOException {
//读操作
String encoding="GBK";
File f = new File("d:/myFile.txt");
if(f.isFile() && f.exists()){
InputStreamReader read=new InputStreamReader(new FileInputStream(f), encoding);
BufferedReader bufferedreader = new BufferedReader(read);
String lineTXT=null;
while((lineTXT = bufferedreader.readLine()) !=null){
System.out.println(lineTXT.toString().trim());
}
read.close();
}else{
System.out.println("找不到指定的文件!");
}
}
}
public class test1 {
public static void main(String[] args) throws IOException {
String directory="D:";
String fileName="myFile.txt";
File f=new File(directory,fileName);
//判断D盘是否含有myFile.txt文件
if(f.exists()){
System.out.println(f.getAbsolutePath());
System.out.println(f.getName());
System.out.println(f.length());
//写操作
FileWriter fw = new FileWriter(f,true); //设置成true就是追加
fw.write("ee");
fw.write("\r\n");//换行写入
fw.close();
System.out.println(f.length());
}else{
//目录中没有myFile.txt的文件,先创建文件所在的目录
f.getParentFile().mkdirs();
try{
//创建新文件
f.createNewFile();
//写操作
FileWriter fw = new FileWriter(f,true); //设置成true就是追加
fw.write("ee");
fw.write("\r\n");
fw.flush();
fw.close();
System.out.println(f.length());
}catch(IOException e){
System.out.println("创建新文件时出现了错误。。。");
e.printStackTrace();
}
}
}
}
【把txt文件按行形式读出来】
public class read {
public static void main(String[] args) throws IOException {
//读操作
String encoding="GBK";
File f = new File("d:/myFile.txt");
if(f.isFile() && f.exists()){
InputStreamReader read=new InputStreamReader(new FileInputStream(f), encoding);
BufferedReader bufferedreader = new BufferedReader(read);
String lineTXT=null;
while((lineTXT = bufferedreader.readLine()) !=null){
System.out.println(lineTXT.toString().trim());
}
read.close();
}else{
System.out.println("找不到指定的文件!");
}
}
}
阅读全文
0 0
- txt文件的创建与读写
- C# 如何创建与读写txt文件
- C# 如何创建与读写txt文件
- C# 如何创建与读写txt文件
- C#与TXT文件的读写操作
- 文件的创建与读写
- C++自动创建命名的txt文件以及读写txt文件
- Java Android 创建读写TXT文件
- cv.Mat 与 .txt 文件数据的读写操作
- C# 1读写Excel 2创建文件读写入txt
- 文件的读写与创建简单例子
- MFC之文件的创建与读写
- SAP ABAP读写Excel 与 TXT文件
- matlab txt文件 矩阵的读写
- C#读写txt文件的方法
- 读写Unicode带中文的txt文件
- ini和txt文件的读写
- java读写TXT文件的方法
- mysql5.7.17日志时间戳(log_timestmaps)与系统时间不一致问题以及日志报Got an error reading communication packets情况分析
- linux 下打包和解压命令
- 基于Tcp协议的socket例子 linux c语言
- Md5
- Tag,Layer跟SortingLayer
- txt文件的创建与读写
- [spark] TaskScheduler 任务提交与调度源码解析
- fillna函数
- Jackson json 处理全大写或不规范的JSON
- C++ 递归算法
- SQLScount插件破解激活
- python: numpy--函数 shape用法
- R语言 igraph——图挖掘助力社会网络分析
- winequality-white