Java如何读取和记录记事本中的内容?? 155
最好把代码写出来!!!!我是想做一个servlet,账号和密码记录在记事本中,在登录是用java读取记事本的内容来判断登录框中的密码是否正确...
最好把代码写出来!!!!
我是想做一个servlet,账号和密码记录在记事本中,在登录是用java读取记事本的内容来判断登录框中的密码是否正确 展开
我是想做一个servlet,账号和密码记录在记事本中,在登录是用java读取记事本的内容来判断登录框中的密码是否正确 展开
4个回答
展开全部
假设C:\test.txt的内容有三行,分别是123、456、789,那么:
1、一次性读文件的全部内容:
123
456
789
-----------------------------------
2、逐行读取:
第 1 行:123
第 2 行:456
第 3 行:789
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TxtReader {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String filename = "c:\\test.txt";
System.out.println("1、一次性读文件的全部内容:\n" + readAll(filename));
System.out.println("-----------------------------------");
System.out.println("2、逐行读取:");
List<String> texts = readLine(filename);
for(int i=0; i<texts.size(); i++ ) {
System.out.print("第 " + (i+1) + " 行:");
System.out.println(texts.get(i));
}
}
public static String readAll(String filename) {
String text = "";
try {
FileInputStream filein=new FileInputStream(filename);
byte[] b = new byte[filein.available()];
filein.read(b);
text = new String(b);
filein.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return text;
}
public static List<String> readLine(String filename) {
List<String> texts = new ArrayList<String>();
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String temp = "";
while((temp=br.readLine()) != null) {
texts.add(temp);
}
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return texts;
}
}
1、一次性读文件的全部内容:
123
456
789
-----------------------------------
2、逐行读取:
第 1 行:123
第 2 行:456
第 3 行:789
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TxtReader {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String filename = "c:\\test.txt";
System.out.println("1、一次性读文件的全部内容:\n" + readAll(filename));
System.out.println("-----------------------------------");
System.out.println("2、逐行读取:");
List<String> texts = readLine(filename);
for(int i=0; i<texts.size(); i++ ) {
System.out.print("第 " + (i+1) + " 行:");
System.out.println(texts.get(i));
}
}
public static String readAll(String filename) {
String text = "";
try {
FileInputStream filein=new FileInputStream(filename);
byte[] b = new byte[filein.available()];
filein.read(b);
text = new String(b);
filein.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return text;
}
public static List<String> readLine(String filename) {
List<String> texts = new ArrayList<String>();
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String temp = "";
while((temp=br.readLine()) != null) {
texts.add(temp);
}
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return texts;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
通过查询API,里面有一个类Buffered,他可以帮你做到如何读取和记录记事本的内容.
参考资料: java笔记本
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
读文件:
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++;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
呵呵 记录放数据库中多简单,是不是初学者?
你还是去学习下 java连接数据库吧,很简单
你还是去学习下 java连接数据库吧,很简单
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询