文件操作的第一段代码,Mark一下!
package eveDay.hw151223;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;
public class LyMan {
// 添加留言的功能,只是不断往后面添加
public void addLy(String allPath) {
Scanner input = new Scanner(System.in);
Date date = new Date(System.currentTimeMillis());
File file = new File(allPath);
try {
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
System.out.println("请输入留言标题:");
String topic = input.nextLine();
System.out.println("请输入留言内容:");
String content = input.nextLine();
String time = (1900 + date.getYear()) + "-" + (date.getMonth() + 1)
+ "-" + date.getDate() + " " + date.getHours() + ":"
+ date.getMinutes() + ":" + date.getSeconds();
System.out.println("当前时间:" + time + "\n留言信息已经保存");
bw.write(topic + "|" + time + "|" + content);
bw.newLine();
bw.close();
fw.close();
} catch (IOException e) {
System.out.println("文件不可写,改为可写!");
file.setWritable(true);
System.out.println("已改为可写!");
}
}
// 显示全部留言
public void showLy(String pathname) {
File file = new File(pathname);
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
if (line == null) {
System.out.println("没有留言信息!");
}
int count = 1;
while (line != null) {
String[] lines = line.split("\\|");
System.out.println("序号:" + count + "\t标题:" + lines[0] + "\t时间:"
+ lines[1] + "\n内容:" + lines[2]
+ "\n-------------------------------------------\n\n");
count++;
line = br.readLine();
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
System.out.println("没有文件,新建留言板文件!");
try {
file.createNewFile();
} catch (IOException e1) {
System.out.println("文件不可写,改为可写!");
file.setWritable(true);
System.out.println("已改为可写!");
}
} catch (IOException e) {
System.out.println("文件不可写,改为可写!");
file.setWritable(true);
System.out.println("已改为可写!");
}
}
public static void main(String[] args) {
String prjPath = System.getProperty("user.dir");
String fileName = "messageboard.txt";
LyMan lyman = new LyMan();
System.out.println("欢迎来到本站点进行留言");
exit: while (true) {
System.out.println("1.发布留言\t2.查询留言\t3.退出系统");
Scanner input = new Scanner(System.in);
int choice = 0;
try {
choice = input.nextInt();
} catch (InputMismatchException e) {
System.out.println("输入错误请重新输入:");
choice = 0;
}
switch (choice) {
case 1:
lyman.addLy(prjPath + "\\" + fileName);
break;
case 2:
lyman.showLy(prjPath + "\\" + fileName);
break;
case 3:
System.out.println("退出系统");
break exit;
default:
System.out.println("功能项选择错误,请重新输入!");
break;
}
}
}
}
可能会出现的错误:
1.输入时,添加“|”会出现结果错误。解决方法,把输入部分,单独做一个方法,在方法内解决这些问题。
private String inputString() {
Scanner input = new Scanner(System.in);
String inputs = "";
while (true) {
inputs = input.nextLine();
if (inputs.contains("|")) {
System.out.println("含有非法字符“|”,请重新输入!!!");
} else {
break;
}
}
return inputs;
}
2。当保存的留言数据比较多时,Console里面可能会出现显示错误的问题。解决方案:更改Console的设置,Fixed width console->Maximum character width:80或者其他数值。取消勾选Limit console output..
目前就只找到这些BUG。