惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hugging Face - Blog
Hugging Face - Blog
D
DataBreaches.Net
A
About on SuperTechFans
D
Docker
腾讯CDC
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
W
WeLiveSecurity
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs
V
Vulnerabilities – Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
P
Privacy International News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Application and Cybersecurity Blog
Application and Cybersecurity Blog
aimingoo的专栏
aimingoo的专栏
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Secure Thoughts
Stack Overflow Blog
Stack Overflow Blog
T
Tenable Blog
T
Threatpost
P
Proofpoint News Feed
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
N
News and Events Feed by Topic
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
Hacker News: Ask HN
Hacker News: Ask HN
B
Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Microsoft Azure Blog
Microsoft Azure Blog
F
Fortinet All Blogs
Project Zero
Project Zero
Help Net Security
Help Net Security
WordPress大学
WordPress大学
F
Full Disclosure
博客园 - 三生石上(FineUI控件)
O
OpenAI News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
Security Latest
Security Latest
T
Tor Project blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog

星空下的YZY

【生活随笔】终于可以试一试new Bing了 指定日期网页自动变灰 【学习记录】微信小程序tabBar与自定义navigationBar踩坑记录 这小小破站(摸鱼)一周年啦 【个人记录参考】Vue3搭建项目 【学习记录】CSS动画 【随笔】将备案接入服务商转为腾讯云 正则表达式 【随笔】模拟退火 【随笔】博弈题基础回顾 live2d-widget(看板娘)修改及部署至服务器 【生活随笔】又是一年226,祝自己生日快乐 【杂记】各类零碎笔记(不定期更新) 友链朋友圈4.X.X前端适配尝试【基于店长Akilar的旧版方案】 使用JDBC操作数据库 Hexo中使用ECharts动态图表尝试 【随笔】通过必应的工具发现别的网站未经允许的转载 【暂不公开】YZY的ACM模板整理-java版 更换Butterfly主题及其美化记录
Java读写txt文件
星空下的YZY · 2022-01-31 · via 星空下的YZY

前言:

最近翻阅我电脑中我过去写的代码的时候,发现以前尝试过用java读写txt文件,不过现在我也基本忘了,所以就有了这一篇🤣以免我以后有需要用到又四处查有关资料。

如有错误,欢迎大佬留言指正🤣。

【转载说明】本文优先发布于我的个人博客www.226yzy.com ,转载请注明出处并注明作者:星空下的YZY。

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0许可协议。

有关代码总览

有关代码总览,及部分注释如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.*;
import java.util.*;

public class Main
{
public static void main(String[] args) throws IOException
{

File file=new File("text.txt");

file.createNewFile();


InputStreamReader fReader = new InputStreamReader(new FileInputStream(file),"UTF-8");
BufferedReader br = new BufferedReader(fReader);

String line;
System.out.println("------当前文件中的内容------");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();


Scanner sc=new Scanner(System.in);
System.out.println("请输入一行内容:");
String value=sc.nextLine();
FileOutputStream writerStream=new FileOutputStream(file,true);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));
bw.write(value);
bw.close();
sc.close();
}
}

下文将分别讲

创建File对象

该部分代码如下:

1
2
3
4

File file=new File("text.txt");

file.createNewFile();

text.txt可以替换成你文件的路径

file.createNewFile();会按照路径尝试创建该文件,如果该文件存在就不会重复创建。

读取内容

该部分代码及部分注释如下:

1
2
3
4
5
6
7
8
9
10

InputStreamReader fReader = new InputStreamReader(new FileInputStream(file),"UTF-8");
BufferedReader br = new BufferedReader(fReader);

String line;
System.out.println("------当前文件中的内容------");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();

这里特别注意可能出现的中文乱码的问题

首先要保证读写两个功能使用的字符集最好一致,否则仍有可能出现乱码

另外,手动在txt文件中输入中文,貌似我这么操作的情况下用UTF-8读取时中文会变成?,使用GB2312则可以正常显示中文😂。

写入内容

该部分代码及部分注释如下:

1
2
3
4
5
6
7
8
9

Scanner sc=new Scanner(System.in);
System.out.println("请输入一行内容:");
String value=sc.nextLine();
FileOutputStream writerStream=new FileOutputStream(file,true);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));
bw.write(value);
bw.close();
sc.close();

同样要注意可能出现的中文乱码问题,具体见上文

FileOutputStream writerStream=new FileOutputStream(file,true);true表示在原来的内容上追加,若该值为false则表示覆盖原内容。

最后

暂时就写到这了,如有错误,欢迎大佬留言指正🤣。

欢迎访问我的小破站https://www.226yzy.com/ 或者GitHub版镜像 https://226yzy.github.io/ 或Gitee版镜像https://yzy226.gitee.io/

我的Github:226YZY (星空下的YZY) (github.com)

【转载说明】本文优先发布于我的个人博客www.226yzy.com ,转载请注明出处并注明作者:星空下的YZY。

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0许可协议。

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 星空下的YZY

打赏

  • 微信

    微信

  • 支付宝

    支付宝