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

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
D
DataBreaches.Net
Microsoft Security Blog
Microsoft Security Blog
U
Unit 42
V
Visual Studio Blog
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
博客园 - Franky
C
CXSECURITY Database RSS Feed - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
P
Privacy & Cybersecurity Law Blog
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
I
Intezer
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
腾讯CDC
博客园 - 【当耐特】
Know Your Adversary
Know Your Adversary
TaoSecurity Blog
TaoSecurity Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Fortinet All Blogs
Project Zero
Project Zero
Blog — PlanetScale
Blog — PlanetScale
S
Security @ Cisco Blogs
量子位
M
MIT News - Artificial intelligence
美团技术团队
C
Cisco Blogs
S
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
Google Developers Blog
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
The Hacker News
The Hacker News
H
Help Net Security
S
Secure Thoughts
Scott Helme
Scott Helme
SecWiki News
SecWiki News
T
Troy Hunt's Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 叶小钗
O
OpenAI News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 司徒正美
T
Tenable Blog

qubeijun的博客

言语理解与表达 父亲往事-我儿子叫冬冬 公务员行测答题顺序及用时及答题技巧 判断推理 数量关系 冬甜夫妇5-10-5-21日本蜜月行程安排回归总结篇(无图片) 冬甜夫妇5.10-5.21日本蜜月行程安排计划篇 5.1 小贝壳疫苗计划 100DAYS 父亲往事-猴牤大爷的“帽子” 父亲往事-姨妈家的葡萄架 父亲往事-十五的月饼 父亲往事-姑妈的压岁钱 父亲往事-母亲的缝纫机父亲的锯 父亲往事-家属来队 父亲往事-新兵连的生活 父亲往事-村北的槐树林 父亲往事-小贝壳 公务员面试 系统架构设计师 图形推理 数学运算 父亲往事-惊蛰 资料分析 父亲往事-偷吃西红柿有绝窍 父亲往事-毯子衣服 父亲往事-水壶煮鸡蛋 父亲往事-煎饼蘸香油 系统架构设计师论文 软件设计师 父亲往事-大哥 资料分析-旧 基于grafana的监控部署 mysql数据库定时备份&运行日志定时清理 各种问题解决答案汇总 Alibaba Cloud Toolkit的使用说明 Dojo简单入门 Angular简单入门 vue基础知识点总结 java正则表达式收集 java处理时间和日期api java常见集合汇总 java调试代码不再用system.out.println(),用日志来调试程序 java常用工具类总结 java常用字符串操作总结 python之pymysql增删改查mysql数据库 词性标注集 python获取当前文件夹下的所有的文件名 mysql数据库操作总结 (翻译)Understanding LSTM Networks (翻译)Understanding Convolutional Neural Networks for NLP (翻译)传统和深度学习模型在文本分类中的应用综述与基准 小木虫论坛-学术科研互动平台爬虫 solr命令(持续更新) python json读写操作 python pip安装包镜像 python数字进度条 抓取维基百科分类索引下某一分类下的子分类及其分类下的页面(仅限词条) python3循环读取excel文件并写入json python3 requests动态网页post提交数据 python+selenium搜狗微信主页抓取 2017年5月25日 2017年5月18日 2017年5月17日 2017年5月15日 2017年5月16日 2017年5月14日 2017年5月13日 2017年5月12日 2017年5月11日 2017年5月10日 2017年5月9日 2017年5月8日 2017年5月7日 2017年5月6日 2017年5月5日 2017年5月4日 2017年5月3日 2017年5月2日 2017年5月1日 儿子的信-2013年7月9日 儿子的信-2013年7月8日 儿子的信-2011年11月26日 儿子的信-2010年8月16日 儿子的信-2010年1月2日
java常用的io,输入输出
qubeijun · 2020-02-28 · via qubeijun的博客

发表于|更新于|java

|总字数:1.4k|阅读时长:7分钟|浏览量:|

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package _8javaio;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class Main {
public static byte[] getFileDataAsBytes(File f)
{
byte[] data = new byte[(int)f.length()];
try(InputStream is =new FileInputStream(f))
{
int n;
while((n=is.read(data))!=-1)
{
System.out.println("read"+n+"bytes");
}

}catch(IOException e)
{
e.printStackTrace();
}
return data;
}
static void printFiles(File[] files) {
System.out.println("==========");
if (files != null) {
for (File f : files) {
System.out.println(f);
}
}
System.out.println("==========");
}

public static void main(String[] args) throws Exception{
//File对象
File f = new File("..");
System.out.println(f.getPath()); //构造方法传入路径
System.out.println(f.getAbsolutePath()); //返回绝对路径
System.out.println(f.getCanonicalPath());//返回规范绝对路径
System.out.println(File.separator); //返回当前平台系统路径分隔符

// File f1 = new File("C:\\Windows");
// File f2 = new File("C:\\Windows\\notepad.exe");
// File f3 = new File("C:\\Windows\\nothing");
// System.out.println(f1.isFile()); //false 文件
// System.out.println(f1.isDirectory()); //true 目录
// System.out.println(f2.isFile()); //true
// System.out.println(f2.isDirectory()); //false
// System.out.println(f3.isFile()); //false
// System.out.println(f3.isDirectory()); //false


//文件权限和大小
// System.out.println(f2.canRead()); //是否可读
// System.out.println(f2.canWrite()); //是否可写
// System.out.println(f2.canExecute());//是否可执行
// System.out.println(f2.length()); //文件字节大小

//创建删除文件
File file = new File("test/test.txt"); //与src同一级
if (file.createNewFile()) {
// 文件创建成功:
System.out.println("create success");
System.out.println(file.getAbsolutePath());
// TODO:
if (file.delete()) {
// 删除文件成功:
System.out.println("delete success");
}
}

//创建临时文件
// File f4 = File.createTempFile("tmp-", ".txt"); // 提供临时文件的前缀和后缀
// f4.deleteOnExit(); // JVM退出时自动删除
// System.out.println(f4.isFile());
// System.out.println(f4.getAbsolutePath());

//遍历文件和目录
// File f5 = new File("C:Windows");
//
// File[] fs1 = f5.listFiles(); // 列出所有文件和子目录
// printFiles(fs1);
// File[] fs2 = f5.listFiles(new FilenameFilter() { // 仅列出.exe文件
// public boolean accept(File dir, String name) {
// return name.endsWith(".exe"); // 返回true表示接受该文件
// }
// });
// printFiles(fs2);

//创建和删除目录
new File("hello").mkdir();
new File("hello/le").mkdirs();
new File("hello/le").delete();
new File("hello").delete();


//Path
Path p1 = Paths.get(".", "project", "study"); // 构造一个Path对象
System.out.println(p1);
Path p2 = p1.toAbsolutePath(); // 转换为绝对路径
System.out.println(p2);
Path p3 = p2.normalize(); // 转换为规范路径
System.out.println(p3);
File f6 = p3.toFile(); // 转换为File对象
System.out.println(f6);
for (Path p : Paths.get("..").toAbsolutePath()) { // 可以直接遍历Path
System.out.println(" " + p);
}


/**InputStream*/
//字节读文件
// 创建一个FileInputStream对象:
File file2 = new File("test/readme.txt");
file2.createNewFile();
try (InputStream input = new FileInputStream("test/readme.txt")) {
int n;
while ((n = input.read()) != -1) {
System.out.println(n);
}
} // 编译器在此自动为我们写入finally并调用close()


//缓冲读文件
try (InputStream input = new FileInputStream("test/readme.txt")) {
// 定义1000个字节大小的缓冲区:
byte[] buffer = new byte[1000];
int n;
while ((n = input.read(buffer)) != -1) { // 读取到缓冲区
System.out.println("read " + n + " bytes.");
}
}
//模拟InputStream
String s;
byte[] data = { 72, 101, 108, 108, 111, 33 };
try (InputStream input = new ByteArrayInputStream(data)) {
int n;
StringBuilder sb = new StringBuilder();
while ((n = input.read()) != -1) {
sb.append((char) n);
System.out.println((char)n);
}
s = sb.toString();
}
System.out.println(s);


/**OutputStream*/
//一次写入一个字节
OutputStream output = new FileOutputStream("test/readme.txt");
output.write(72); // H
output.write(101); // e
output.write(108); // l
output.write(108); // l
output.write(111); // o
output.close();

//一次写入若干字节
try (OutputStream output1 = new FileOutputStream("test/readme.txt")) {
output1.write("Hello!".getBytes("UTF-8")); // Hello
} // 编译器在此自动为我们写入finally并调用close()

//模拟OutputStream
byte[] data3;
try (ByteArrayOutputStream output3 = new ByteArrayOutputStream()) {
output3.write("Hello ".getBytes("UTF-8"));
output3.write("world!".getBytes("UTF-8"));
data3 = output3.toByteArray();
}
System.out.println(new String(data3, "UTF-8"));


//Filter模式
InputStream file7 = new FileInputStream("test/test.gz");
InputStream buffered = new BufferedInputStream(file7);
InputStream gzip = new GZIPInputStream(buffered);
System.out.println(gzip);
file7.close();

/**操作Zip*/
//读取zip
try (ZipInputStream zip = new ZipInputStream(new FileInputStream("test/test.zip"))) {
ZipEntry entry = null;
while ((entry = zip.getNextEntry()) != null) {
String name = entry.getName();
System.out.println("name:"+name);
if (!entry.isDirectory()) {
int n;
while ((n = zip.read()) != -1) {
System.out.println(n);
}
}
}
}
//写入zip
try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream("test/log.zip"))) {
File ff = new File("log");
File[] files = ff.listFiles();
for (File file1 : files) {
zip.putNextEntry(new ZipEntry(file1.getName()));
zip.write(getFileDataAsBytes(file1));
zip.closeEntry();
}
}

/**序列化*/
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try (ObjectOutputStream output1 = new ObjectOutputStream(buffer)) {
// 写入int:
output1.writeInt(12345);
// 写入String:
output1.writeUTF("Hello");
// 写入Object:
output1.writeObject(Double.valueOf(123.456));
}
System.out.println(Arrays.toString(buffer.toByteArray()));


/**反序列化*/
ByteArrayInputStream input1 = new ByteArrayInputStream(buffer.toByteArray());
try (ObjectInputStream input2 = new ObjectInputStream(input1)) {
int n1 = input2.readInt();
String s1 = input2.readUTF();
Double d1 = (Double) input2.readObject();
System.out.println(n1+s1+d1);
}


/**Reader 字符流*/
//FileReader
try (Reader reader = new FileReader("test/test.txt")) {
char[] buffer1 = new char[1000];
int n;
while ((n = reader.read(buffer1)) != -1) {
System.out.println("read " + n + " chars.");
}
}
//CharArrayReader 模拟Reader
try (Reader reader = new CharArrayReader("Hello".toCharArray())) {
System.out.println(reader);
}
//StringReader
try (Reader reader = new StringReader("Hello")) {
System.out.println(reader);
}
//InputStreamReader
try (Reader reader = new InputStreamReader(new FileInputStream("test/test.txt"), "UTF-8")) {
// TODO:
System.out.println(reader);
}

/**Wirter 字符流*/
//FileWriter
try (Writer writer = new FileWriter("test/test.txt")) {
writer.write('H'); // 写入单个字符
writer.write("Hello".toCharArray()); // 写入char[]
writer.write("Hello"); // 写入String
writer.write("你好");
}

//CharArrayWriter
try (CharArrayWriter writer = new CharArrayWriter()) {
writer.write(65);
writer.write(66);
writer.write(67);
char[] data4 = writer.toCharArray(); // { 'A', 'B', 'C' }
System.out.println(data4);
}

//StringWriter
//OutputStreamWriter
try (Writer writer = new OutputStreamWriter(new FileOutputStream("test/test.txt"), "UTF-8")) {
// TODO:
System.out.println(writer);
}

/**PrintStream和PrintWriter*/
System.out.print(12345); // 输出12345
System.out.print(new Object()); // 输出类似java.lang.Object@3c7a835a
System.out.println("Hello"); // 输出Hello并换行

StringWriter buffer4 = new StringWriter();
try (PrintWriter pw = new PrintWriter(buffer4)) {
pw.println("Hello");
pw.println(12345);
pw.println(true);
}
System.out.println(buffer4.toString());

}
}

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

赞助

  • 微信

    微信

  • 支付宝

    支付宝