IO流问题,怎么把,,数组中的数据写入文件

不忘初心,方能秃头 2018-08-20 04:45:13
做了,,一下午了,,,可能是对io流使用的很不熟练,,尝试过好几种思路还没实现,, 希望大神,,有时间给个思路清晰的,答案,,,谢谢谢鞋了。。。。
...全文
1167 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_57040768 2021-08-09
  • 打赏
  • 举报
回复
如何把读取的数值存放到数组,
qq_39936465 2021-08-13
  • 举报
回复
@qq_57040768 用split分割,然后string转其他类型保存
qq_39936465 2018-09-25
  • 打赏
  • 举报
回复
说下思路

看题意

应该是用下面形式进行文件读取

File f= new File(文件名称);
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);

增减操作建议建立一个list

List<String> list = new ArrayList();

while((String str=br.readLine())!=null){
list.add(str);

这样能直接逐行读取文件
用List进行增减操作比较方便。写入也方便如下:

FileWriter fw=new FileWriterr(f);
BufferedWriter bw=new BufferedWriter(fr);

for (String i:list) bw.write(i);

List比较灵活,你还可以建一个Person的类包含(名字,性别,电话号码)

List<Person> list= new ListArray();
进行类操作。
  • 打赏
  • 举报
回复
谢谢大佬们,,哥哥们,,我会继续努力的,,希望大佬们工作越来越好,,谢谢谢谢,,
  • 打赏
  • 举报
回复
引用 3 楼 逃离计划的回复:
import java.io.*;

/**
* 数据源文件信息:
* 张三;男;1529366;
* 小米;女;13;
* lili;女;11111;
* <p>
* 数据源要求:以';'分割属性
*/
public class IO {

//数据源条数
static final int COUNT = 3;
//保存从文件中读取到的信息;
static String[][] phoneArrs = new String[COUNT][];

//创建数据源文件对象(从该文件对象读取信息)
static File readFiel = new File("src/phoneData.txt");
//创建输出文件对象(保存输出的信息)
static File writeFile = new File("src/phoneBook.txt");

//输出字节流
static OutputStream out;
//输入缓冲流
static BufferedReader br;

/**
* @Author: hyh
* 读取文件信息
*/
static void readBook() {
try {
//实例化输入流对象
br = new BufferedReader(new FileReader(readFiel));
//用于保存读取到的字符串(readLine每次读取一行)
String str;
//循环读取文件,直到文件末尾
for (int i = 0; i < COUNT && (str = br.readLine()) != null; i++) {
String[] strings;
//按';'分割字符串,得到一个新的字符串数组
strings = str.split(";");
//把得到的字符串数组存放到phoneBook中
phoneArrs[i] = strings;
}
//关闭流
br.close();
System.out.println("读取完成");
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* @Author: hyh
* 把数组信息写到文件中
*/
static void writeBook() {
String[] temp; //表示一条联系人信息
try {
//实例化输出流对象(追加信息)
out = new FileOutputStream(writeFile, true);

for (int i = 0; i < phoneArrs.length; i++) {
temp = phoneArrs[i];
//写入一条信息
for (int j = 0; j < temp.length; j++) {
out.write(temp[j].getBytes());
//添加符号
out.write(';');
}
//每条信息写入完成后换行
out.write('\n');
}
//关闭流
out.flush();
out.close();
System.out.println("写入完成");
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
readBook();
//读取完成后,对数据进行修改
phoneArrs[1][1] = "修改的数据111";
//修改完成,写入文件
writeBook();
}

}
谢谢,谢谢,,其实,,,后来我的思路,,和你实现的思路一样,,,之前被,,某些东西限制住了,,不过看你的代码写的就是比我写的好。。。 非常感谢百忙之中帮我解决问题,,非常感谢非常感谢,非常感谢,,最后祝工作顺利生活幸福,。
hyhssm 2018-08-20
  • 打赏
  • 举报
回复
import java.io.*;

/**
* 数据源文件信息:
* 张三;男;1529366;
* 小米;女;13;
* lili;女;11111;
* <p>
* 数据源要求:以';'分割属性
*/
public class IO {

//数据源条数
static final int COUNT = 3;
//保存从文件中读取到的信息;
static String[][] phoneArrs = new String[COUNT][];

//创建数据源文件对象(从该文件对象读取信息)
static File readFiel = new File("src/phoneData.txt");
//创建输出文件对象(保存输出的信息)
static File writeFile = new File("src/phoneBook.txt");

//输出字节流
static OutputStream out;
//输入缓冲流
static BufferedReader br;

/**
* @Author: hyh
* 读取文件信息
*/
static void readBook() {
try {
//实例化输入流对象
br = new BufferedReader(new FileReader(readFiel));
//用于保存读取到的字符串(readLine每次读取一行)
String str;
//循环读取文件,直到文件末尾
for (int i = 0; i < COUNT && (str = br.readLine()) != null; i++) {
String[] strings;
//按';'分割字符串,得到一个新的字符串数组
strings = str.split(";");
//把得到的字符串数组存放到phoneBook中
phoneArrs[i] = strings;
}
//关闭流
br.close();
System.out.println("读取完成");
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* @Author: hyh
* 把数组信息写到文件中
*/
static void writeBook() {
String[] temp; //表示一条联系人信息
try {
//实例化输出流对象(追加信息)
out = new FileOutputStream(writeFile, true);

for (int i = 0; i < phoneArrs.length; i++) {
temp = phoneArrs[i];
//写入一条信息
for (int j = 0; j < temp.length; j++) {
out.write(temp[j].getBytes());
//添加符号
out.write(';');
}
//每条信息写入完成后换行
out.write('\n');
}
//关闭流
out.flush();
out.close();
System.out.println("写入完成");
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
readBook();
//读取完成后,对数据进行修改
phoneArrs[1][1] = "修改的数据111";
//修改完成,写入文件
writeBook();
}

}
沁海棠 2018-08-20
  • 打赏
  • 举报
回复
按照行读取出来,切割封装为一个bean,bean的属性有,姓名,性别,电话等。。。然后看你具体要求操作
鱿鱼ing 2018-08-20
  • 打赏
  • 举报
回复
说说你遇到的难点

写入文件应该很简单吧 工具类很多 数组的数据可以循环调用这个方法 只要注意追加覆盖的区别
如下:

file是文件路径 content是内容
public static void write(String file, String conent) throws IOException {
File file2 = new File(file);
if (!file2.exists()) {
file2.createNewFile();
}
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
out.write(conent + "\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧