我可以在 Java 中的同一个 class 中编写 FileWriter 和 bufferedwriter 吗?
Can I write FileWriter and bufferedwriter in a same class in Java?
这是我尝试使用 Filewriter 编写的代码。这工作正常。
File f2 = new File("Path");
f2.createNewFile();
FileWriter writing = new FileWriter(f2);
writing.write("i'm into you , i'm into you");
writing.flush();
在下面的代码中,我尝试使用 bufferedwriter 来编写。这不是在同一个文件中添加任何文本。对于不同的文件,它正在工作。
BufferedWriter buffwrite = new BufferedWriter(writing); buffwrite.write("java");
writing.flush();
这些IO相关class是基于装饰器模式设计的。
如果您参考 BufferedWriter
class javadoc,您会发现一个采用 Writer
类型对象的构造函数。 Writer
是一个抽象 class,由 FileWriter
和其他 class 扩展。在构造函数中传递您的 FileWriter
对象,然后调用 BufferedWriter
.
的 write (...)
和 flush
方法
所有 IO class都以这种模式工作。
是的 你可以write.Please检查以下用例在java中使用FileWriter、BufferedWriter、FileOutputStream和文件在java中写入文件].
package com.journaldev.files;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class WriteFile {
/**
* This class shows how to write file in java
* @param args
* @throws IOException
*/
public static void main(String[] args) {
String data = "I will write this String to File in Java";
int noOfLines = 10000;
writeUsingFileWriter(data);
writeUsingBufferedWriter(data, noOfLines);
writeUsingFiles(data);
writeUsingOutputStream(data);
System.out.println("DONE");
}
/**
* Use Streams when you are dealing with raw data
* @param data
*/
private static void writeUsingOutputStream(String data) {
OutputStream os = null;
try {
os = new FileOutputStream(new File("/Users/pankaj/os.txt"));
os.write(data.getBytes(), 0, data.length());
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Use Files class from Java 1.7 to write files, internally uses OutputStream
* @param data
*/
private static void writeUsingFiles(String data) {
try {
Files.write(Paths.get("/Users/pankaj/files.txt"), data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Use BufferedWriter when number of write operations are more
* It uses internal buffer to reduce real IO operations and saves time
* @param data
* @param noOfLines
*/
private static void writeUsingBufferedWriter(String data, int noOfLines) {
File file = new File("/Users/pankaj/BufferedWriter.txt");
FileWriter fr = null;
BufferedWriter br = null;
String dataWithNewLine=data+System.getProperty("line.separator");
try{
fr = new FileWriter(file);
br = new BufferedWriter(fr);
for(int i = noOfLines; i>0; i--){
br.write(dataWithNewLine);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Use FileWriter when number of write operations are less
* @param data
*/
private static void writeUsingFileWriter(String data) {
File file = new File("/Users/pankaj/FileWriter.txt");
FileWriter fr = null;
try {
fr = new FileWriter(file);
fr.write(data);
} catch (IOException e) {
e.printStackTrace();
}finally{
//close resources
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
是的,您绝对可以将两者写在同一个 class 中。
您的代码没有将任何文本添加到同一文件中,因为您在 BufferedWriter 之前刷新 FileWriter。我刚刚按如下方式编辑了您的代码,它运行良好。
File f2 = new File("Path");
f2.createNewFile();
FileWriter writing = new FileWriter(f2);
writing.write("i'm into you , i'm into you");
BufferedWriter buffwrite = new BufferedWriter(writing);
buffwrite.write("java");
buffwrite.flush();//flush BufferedWriter first followed by FileWriter
writing.flush();
您应该 close
您打开的资源:FileWriter writing
和 BufferedWriter buffwrite
。这个(JavaDoc)
Flushes the stream. If the stream has saved any characters from the
various write() methods in a buffer, write them immediately to their
intended destination. Then, if that destination is another character
or byte stream, flush it. Thus one flush() invocation will flush all
the buffers in a chain of Writers and OutputStreams.
正确的方法是使用try-resource语句。这将关闭打开的资源。
或者使用 java.nio.file.Files.write
方法进行资源处理。
这是我尝试使用 Filewriter 编写的代码。这工作正常。
File f2 = new File("Path");
f2.createNewFile();
FileWriter writing = new FileWriter(f2);
writing.write("i'm into you , i'm into you");
writing.flush();
在下面的代码中,我尝试使用 bufferedwriter 来编写。这不是在同一个文件中添加任何文本。对于不同的文件,它正在工作。
BufferedWriter buffwrite = new BufferedWriter(writing); buffwrite.write("java");
writing.flush();
这些IO相关class是基于装饰器模式设计的。
如果您参考 BufferedWriter
class javadoc,您会发现一个采用 Writer
类型对象的构造函数。 Writer
是一个抽象 class,由 FileWriter
和其他 class 扩展。在构造函数中传递您的 FileWriter
对象,然后调用 BufferedWriter
.
write (...)
和 flush
方法
所有 IO class都以这种模式工作。
是的 你可以write.Please检查以下用例在java中使用FileWriter、BufferedWriter、FileOutputStream和文件在java中写入文件].
package com.journaldev.files;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class WriteFile {
/**
* This class shows how to write file in java
* @param args
* @throws IOException
*/
public static void main(String[] args) {
String data = "I will write this String to File in Java";
int noOfLines = 10000;
writeUsingFileWriter(data);
writeUsingBufferedWriter(data, noOfLines);
writeUsingFiles(data);
writeUsingOutputStream(data);
System.out.println("DONE");
}
/**
* Use Streams when you are dealing with raw data
* @param data
*/
private static void writeUsingOutputStream(String data) {
OutputStream os = null;
try {
os = new FileOutputStream(new File("/Users/pankaj/os.txt"));
os.write(data.getBytes(), 0, data.length());
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Use Files class from Java 1.7 to write files, internally uses OutputStream
* @param data
*/
private static void writeUsingFiles(String data) {
try {
Files.write(Paths.get("/Users/pankaj/files.txt"), data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Use BufferedWriter when number of write operations are more
* It uses internal buffer to reduce real IO operations and saves time
* @param data
* @param noOfLines
*/
private static void writeUsingBufferedWriter(String data, int noOfLines) {
File file = new File("/Users/pankaj/BufferedWriter.txt");
FileWriter fr = null;
BufferedWriter br = null;
String dataWithNewLine=data+System.getProperty("line.separator");
try{
fr = new FileWriter(file);
br = new BufferedWriter(fr);
for(int i = noOfLines; i>0; i--){
br.write(dataWithNewLine);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Use FileWriter when number of write operations are less
* @param data
*/
private static void writeUsingFileWriter(String data) {
File file = new File("/Users/pankaj/FileWriter.txt");
FileWriter fr = null;
try {
fr = new FileWriter(file);
fr.write(data);
} catch (IOException e) {
e.printStackTrace();
}finally{
//close resources
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
是的,您绝对可以将两者写在同一个 class 中。 您的代码没有将任何文本添加到同一文件中,因为您在 BufferedWriter 之前刷新 FileWriter。我刚刚按如下方式编辑了您的代码,它运行良好。
File f2 = new File("Path");
f2.createNewFile();
FileWriter writing = new FileWriter(f2);
writing.write("i'm into you , i'm into you");
BufferedWriter buffwrite = new BufferedWriter(writing);
buffwrite.write("java");
buffwrite.flush();//flush BufferedWriter first followed by FileWriter
writing.flush();
您应该 close
您打开的资源:FileWriter writing
和 BufferedWriter buffwrite
。这个(JavaDoc)
Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.
正确的方法是使用try-resource语句。这将关闭打开的资源。
或者使用 java.nio.file.Files.write
方法进行资源处理。