📄 savefile.java
字号:
package com;
import java.io.*;
public class SaveFile {
public boolean saveMsg(String[] msg) {
boolean ok = false;
File fo = null;
FileOutputStream to = null;
PrintWriter out = null;
try {
if (!(new File("C:\\Gsm1.0").isDirectory()))
new File("C:\\Gsm1.0").mkdir();// 目标文件夹
fo = new File("C:\\Gsm1.0\\Msgs.txt");
if (!fo.exists()) // 文件不存在
fo.createNewFile();// 创建新文件
to = new FileOutputStream(fo, false); // 创建文件输出流
out = new PrintWriter(to); // 输出流
for (int i = 0; i < msg.length; i++) {
if (msg[i] == null)
break;
out.println(msg[i]);
}
ok = true;
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
return false;
} catch (NullPointerException ee) {
System.out.println(ee.toString());
return false;
} catch (IOException e) {
System.out.println(e.toString());
return false;
} finally {
try {
out.close(); // 关闭流
to.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return ok;
}
public String[] getMsg() {
String[] msg = null;
int i = 0;
FileReader fi = null;
BufferedReader ins = null;
try {
fi = new FileReader("C:\\Gsm1.0\\Msgs.txt");
ins = new BufferedReader(fi);
ins.mark(1024);// 标记流,以便重新读
while (true) { // 分行打印文件数据
if (ins.readLine() == null)
break;
i++;// 记录的数量
}
msg = new String[i + 50];
i = 0;
ins.reset();// 重置流,重读并保存到数组
for (i = 0; i < msg.length; i++) {
if ((msg[i] = ins.readLine()) == null)
break;
}
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
System.out.println("记录文件未找到!") ;
return new String[50];
} catch (NullPointerException e) {
e.printStackTrace();
System.out.println("读取文件出错!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取文件出错!" );
} finally {
try {
fi.close();
ins.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
System.out.println("读取文件出错!");
}
}
return msg;
}
public boolean saveFriend(String[] msg) {
boolean ok = false;
File fo = null;
FileOutputStream to = null;
PrintWriter out = null;
try {
if (!(new File("C:\\Gsm1.0").isDirectory()))
new File("C:\\Gsm1.0").mkdir();// 目标文件夹
fo = new File("C:\\Gsm1.0\\Friends.txt");
if (!fo.exists()) // 文件不存在
fo.createNewFile();// 创建新文件
to = new FileOutputStream(fo, false); // 创建文件输出流
out = new PrintWriter(to); // 输出流
for (int i = 0; i < msg.length; i++) {
if (msg[i] == null)
break;
out.println(msg[i]);
}
ok = true;
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
return false;
} catch (NullPointerException ee) {
System.out.println(ee.toString());
return false;
} catch (IOException e) {
System.out.println(e.toString());
return false;
} finally {
try {
out.close(); // 关闭流
to.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
return false;
}
}
return ok;
}
public String[] getFriend() {
String[] Friend = null;
int i = 0;
FileReader fi = null;
BufferedReader ins = null;
try {
fi = new FileReader("C:\\Gsm1.0\\Friends.txt");
ins = new BufferedReader(fi);
ins.mark(1024);// 标记流,以便重新读
while (true) { // 分行打印文件数据1111111111111111111111111111111111111111111111111111
if (ins.readLine() == null)
break;
i++;// 记录的数量
}
Friend = new String[i+50];
i = 0;
ins.reset();// 重置流,重读并保存到数组
for (i = 0; i < Friend.length; i++) {
if ((Friend[i] = ins.readLine()) == null)
break;
}
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
System.out.println("记录文件未找到!") ;
return new String[50];
} catch (NullPointerException e) {
e.printStackTrace();
System.out.println( "读取文件出错!" );
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取文件出错!" );
} finally {
try {
fi.close();
ins.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
System.out.println( "读取文件出错!" );
}
}
return Friend;
}
public static void main(String[] args) {
SaveFile test = new SaveFile();
String[] str = { "潘", "建", "军" };
String[] get = null;
// test.saveMsg(str);
// get=test.getMsg();
test.saveFriend(str);
get = test.getFriend();
for (int i = 0; i < get.length; i++) {
if (get[i] == null)
break;
System.out.println(get[i]);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -