public
class
f4_add_content_to_txt
{
public
static
void
main
(
String
[
]
args
)
throws
Exception
{
try
{
BufferedWriter
out
=
new
BufferedWriter
(
new
FileWriter
(
"f4.txt"
)
)
;
out
.
write
(
"aString1\n"
)
;
out
.
close
(
)
;
out
=
new
BufferedWriter
(
new
FileWriter
(
"f4.txt"
,
true
)
)
;
out
.
write
(
"aString2"
)
;
out
.
close
(
)
;
BufferedReader
in
=
new
BufferedReader
(
new
FileReader
(
"f4.txt"
)
)
;
String
str
;
while
(
(
str
=
in
.
readLine
(
)
)
!=
null
)
{
System
.
out
.
println
(
str
)
;
in
.
close
(
)
;
catch
(
IOException
e
)
{
System
.
out
.
println
(
"exception occoured"
+
e
)
;
仅作为记录,大佬请跳过。加上,true,即可补加(否则是将原数据删除后,新写)import java.io.*;public class f4_add_content_to_txt { public static void main(String[] args) throws Exception{ try{ BufferedWriter out=new BufferedWriter(new FileWriter("f4.txt"));
import
java
.io.FileInputStream;
import
java
.io.FileOutputStream;
import
java
.io.IOException;
import
java
.io.InputStreamReader;
import
java
.io.PrintW
Hollis ,阿里巴巴技术专家,51CTO 专栏作家,CSDN 博客专家,掘金优秀作者,《程序员的三门课》联合作者,《
Java
工程师成神之路》系列文章作者;热衷于分享计算机编程相关技术,博文全网阅读量数千万
面向对象与面向过程
什么是面向过程
概述: 自顶而下的编程模式
详解:把问题分解成一个一个步骤,每个步骤用函数实现,依次
public static void main(String[] args) {
//
txt
文件
生成的位置
File outFile = new File("D:\\ce\\test.
txt
");
//集合
数据
ArrayList<String> list = new ArrayList<>();
list.add("1");
list.add("2");
Writer..
void f1() throws IOException{
File file = new File("./a.
txt
");
byte[] s="123456".getBytes();
//方法一
OutputStream out=new FileOutputStream(file,true);// 如果为true ,则字节将写入
文件
的末尾而不是开头
out.write(s);
//方法二
RandomAccessFile randomAcces
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import
java
x.servlet.http.HttpServletRequest;
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
* 获取
txt
文件
内容并按行放入list
中
public static List<String> getFileContext(String path) throws Exception {
FileReader fileReader =new FileReader(path);
Buffe...
import
java
.io.BufferedInputStream;
import
java
.io.FileInputStream;
import
java
.io.IOException;
import
java
.io.InputStream;
public class BufferedInputStreamExample {
public static void main(String[] args) {
try {
InputStream inputStream = new FileInputStream("example.
txt
");
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
byte[] buffer = new byte[8192]; // 缓冲区大小为 8192 字节
int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
// 处理读取到的
数据
,buffer 数组
中
前 bytesRead 个字节是有效
数据
// 这里可以根据需要进行操作,例如写入到其他输出流或进行
数据
处理
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
在上面的代码
中
,我们首先创建了一个 `FileInputStream` 对象来读取
文件
"example.
txt
"。然后,我们使用 `BufferedInputStream` 对象包装该输入流,
增加
了缓冲区。
在 `while` 循环
中
,我们使用 `bufferedInputStream.read(buffer)` 方法来读取
数据
。该方法会将最多 `buffer.length` 个字节的
数据
读入 `buffer` 数组
中
,并返回实际读取的字节数。当返回值为 -1 时,表示已经读到了流的末尾。
您可以在循环
中
对读取到的
数据
进行处理,例如写入到其他输出流或进行进一步的
数据
处理操作。
最后,不要忘记在使用完毕后关闭流对象以释放资源。在示例代码
中
,我们调用了 `bufferedInputStream.close()` 方法来关闭流。
请注意,根据实际需求,您可能需要适当调整缓冲区的大小。过小的缓冲区可能导致频繁的 I/O 操作,过大的缓冲区则可能占用过多的内存。因此,根据具体情况进行测试和调整是很重要的。