怎样在Java中使用记事本存取数据?
我在用Java编写一个网上购书系统,想把已注册用户的信息(比如账号和密码)存放在记事本文件中,在用户登录时通过查询记事本来判断用户输入的账号和密码是否匹配,我怎样把Jav...
我在用Java编写一个网上购书系统,想把已注册用户的信息(比如账号和密码)存放在记事本文件中,在用户登录时通过查询记事本来判断用户输入的账号和密码是否匹配,我怎样把Java的getText()的内容和记事本的信息链接
展开
展开全部
读文件:
String encoding = "gbk";
File file = new File("F:\\xxx.txt");
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
list = new ArrayList<String>();
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
if (lineTxt != "") {
list.add(lineTxt);
}
}
read.close();
}
写文件:
File outFile = new File(parentFile, inFile.getName() + outFileIndex
+ SUFFIX);
FileOutputStream out = new FileOutputStream(outFile);
inEndIndex += size;
inEndIndex = (inEndIndex < fileLength) ? inEndIndex : fileLength;
// 从输入流中读取字节存储到输出流中
OutputStreamWriter write = new OutputStreamWriter(out, "gbk");
BufferedWriter writer = new BufferedWriter(write);
int i = 0;
for (; inBeginIndex < inEndIndex; inBeginIndex++) {
writer.write(reader.read());
if ( (inBeginIndex+1) % 150 == 0 && i<8) {
if (index >= sizeurl) {
index = 0;
}
writer.write(urllist.get(index));
i++;
index++;
}
}
最好是用数据库存储,相对来说安全方便,
String encoding = "gbk";
File file = new File("F:\\xxx.txt");
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
list = new ArrayList<String>();
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
if (lineTxt != "") {
list.add(lineTxt);
}
}
read.close();
}
写文件:
File outFile = new File(parentFile, inFile.getName() + outFileIndex
+ SUFFIX);
FileOutputStream out = new FileOutputStream(outFile);
inEndIndex += size;
inEndIndex = (inEndIndex < fileLength) ? inEndIndex : fileLength;
// 从输入流中读取字节存储到输出流中
OutputStreamWriter write = new OutputStreamWriter(out, "gbk");
BufferedWriter writer = new BufferedWriter(write);
int i = 0;
for (; inBeginIndex < inEndIndex; inBeginIndex++) {
writer.write(reader.read());
if ( (inBeginIndex+1) % 150 == 0 && i<8) {
if (index >= sizeurl) {
index = 0;
}
writer.write(urllist.get(index));
i++;
index++;
}
}
最好是用数据库存储,相对来说安全方便,
2014-05-26
展开全部
通过IO操作txt文件,最好学学用数据库
追问
可以发给我一个实例吗?最好是整个项目包含txt文件,可运行,万分感激
追答
public static void main(String[] args) {
String f = "c:\\1.txt";
try {
File file = new File(f);
file.createNewFile();
FileWriter fw =new FileWriter(f);
fw.write("123\n");
fw.append("1234456");
fw.flush();
fw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String s = br.readLine();
while (s!=null) {
System.out.println(s);
s = br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询