永发信息网

怎样在Java中使用记事本存取数据?

答案:2  悬赏:40  手机版
解决时间 2021-03-28 19:40
  • 提问者网友:眉目添风霜
  • 2021-03-28 03:47
怎样在Java中使用记事本存取数据?
最佳答案
  • 五星知识达人网友:胯下狙击手
  • 2021-03-28 03:58
读文件:

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();
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++;
}
}

最好是用数据库存储,相对来说安全方便,
全部回答
  • 1楼网友:往事埋风中
  • 2021-03-28 05:37
通过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();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯