本文整理了Java中java.io.FileWriter
类的一些代码示例,展示了FileWriter
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileWriter
类的具体详情如下:
包路径:java.io.FileWriter
类名称:FileWriter
[英]A specialized Writer that writes to a file in the file system. All write requests made by calling methods in this class are directly forwarded to the equivalent function of the underlying operating system. Since this may induce some performance penalty, in particular if many small write requests are made, a FileWriter is often wrapped by a BufferedWriter.
[中]向文件系统中的文件写入的专用写入程序。通过调用此类中的方法发出的所有写请求都直接转发到底层操作系统的等效函数。由于这可能会导致一些性能损失,特别是当发出许多小的写入请求时,FileWriter通常由BufferedWriter包装。
canonical example by Tabnine
public void writeToFile(File dest, String content, boolean append) throws IOException {
// append - true for writing to the end of the file rather to the beginning
try (PrintWriter writer = new PrintWriter(new FileWriter(dest, append))) {
writer.print(content);
}
}
代码示例来源:origin: stackoverflow.com
File inputFile = new File("myFile.txt");
File tempFile = new File("myTempFile.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = "bbb";
String currentLine;
while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(inputFile);
代码示例来源:origin: stanfordnlp/CoreNLP
public void produceTrees(String filename, int numTrees) {
try {
FileWriter fout = new FileWriter(filename);
BufferedWriter bout = new BufferedWriter(fout);
PrintWriter pout = new PrintWriter(bout);
produceTrees(pout, numTrees);
pout.close();
bout.close();
fout.close();
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
代码示例来源:origin: apache/zookeeper
public void writeTempDynamicConfigFile(String nextQuorumCfgSection, String version)
throws IOException {
File nextDynamicConfigFile = new File(tmpDir,
"zoo.cfg" + QuorumPeerConfig.nextDynamicConfigFileSuffix);
FileWriter fwriter = new FileWriter(nextDynamicConfigFile);
fwriter.write(nextQuorumCfgSection
+ "\n"
+ "version=" + version);
fwriter.flush();
fwriter.close();
}
代码示例来源:origin: iluwatar/java-design-patterns
/**
* Program entry point
*/
public static void main(String[] args) throws IOException {
FileWriterAction writeHello = writer -> {
writer.write("Hello");
writer.append(" ");
writer.append("there!");
};
new SimpleFileWriter("testfile.txt", writeHello);
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
public void release() throws Exception {
writer.flush();
writer.close();
}
}
代码示例来源:origin: jenkinsci/jenkins
/**
* @since 1.568
*/
protected final void createEmptyChangeLog(@Nonnull File changelogFile, @Nonnull TaskListener listener, @Nonnull String rootTag) throws IOException {
try (FileWriter w = new FileWriter(changelogFile)) {
w.write("<"+rootTag +"/>");
}
}
代码示例来源:origin: Meituan-Dianping/Robust
StringBuilder fileContent = new StringBuilder();
try {
reader = new BufferedReader(new FileReader(file));
String line;
int lineNo = 1;
while ((line = reader.readLine()) != null) {
writer = new BufferedWriter(new FileWriter(file));
writer.write(fileContent.toString());
writer.flush();
} catch (IOException e) {
e.printStackTrace();
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
代码示例来源:origin: stackoverflow.com
try(FileWriter fw = new FileWriter("outfilename", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
out.println("the text");
//more code
out.println("more text");
//more code
} catch (IOException e) {
//exception handling left as an exercise for the reader
}
代码示例来源:origin: alibaba/jstorm
InputStream inputStream = SandBoxMaker.class.getClassLoader().getResourceAsStream(SANBOX_TEMPLATE_NAME);
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmpPolicy)));
BufferedReader reader = null;
InputStreamReader inputReader = null;
try {
inputReader = new InputStreamReader(inputStream);
reader = new BufferedReader(new LineNumberReader(inputReader));
String line;
while ((line = reader.readLine()) != null) {
String replaced = replaceLine(line, replaceMap);
writer.println(replaced);
inputStream.close();
writer.close();
if (reader != null) {
reader.close();
代码示例来源:origin: ReactiveX/RxJava
BufferedReader in = new BufferedReader(new FileReader(u));
try {
for (;;) {
String line = in.readLine();
if (line == null) {
break;
in.close();
PrintWriter w = new PrintWriter(new FileWriter(u));
w.println(s);
w.close();
代码示例来源:origin: apache/geode
protected void writeFilteredLogFile(Path originalLogFile, Path filteredLogFile)
throws IOException {
this.logFilter.startNewFile();
try (BufferedReader reader = new BufferedReader(new FileReader(originalLogFile.toFile()))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filteredLogFile.toFile()))) {
String line;
while ((line = reader.readLine()) != null) {
LogFilter.LineFilterResult result = this.logFilter.acceptsLine(line);
if (result == LogFilter.LineFilterResult.REMAINDER_OF_FILE_REJECTED) {
break;
}
if (result == LogFilter.LineFilterResult.LINE_ACCEPTED) {
writeLine(line, writer);
}
}
}
}
}
代码示例来源:origin: stackoverflow.com
public static void writeStringAsFile(final String fileContents, String fileName) {
Context context = App.instance.getApplicationContext();
try {
FileWriter out = new FileWriter(new File(context.getFilesDir(), fileName));
out.write(fileContents);
out.close();
} catch (IOException e) {
Logger.logError(TAG, e);
}
}
public static String readFileAsString(String fileName) {
Context context = App.instance.getApplicationContext();
StringBuilder stringBuilder = new StringBuilder();
String line;
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(new File(context.getFilesDir(), fileName)));
while ((line = in.readLine()) != null) stringBuilder.append(line);
} catch (FileNotFoundException e) {
Logger.logError(TAG, e);
} catch (IOException e) {
Logger.logError(TAG, e);
}
return stringBuilder.toString();
}
代码示例来源:origin: stackoverflow.com
writer = new BufferedWriter(new FileWriter(this.filepath));
writer().write(chars);
writer().close();
this.reader = new BufferedReader(new FileReader(filepath));
this.read_ahead = this.reader.readLine();
reader = new BufferedReader(new FileReader(filepath));
代码示例来源:origin: org.apache.ant/ant
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(source));
dest.delete();
bw = new BufferedWriter(new FileWriter(dest));
line = br.readLine();
while (line != null) {
if (line.isEmpty()) {
bw.newLine();
} else {
newline = KeySubst.replace(line, replacements);
bw.write(newline);
bw.newLine();
line = br.readLine();
bw.flush();
代码示例来源:origin: stackoverflow.com
import java.io.*;
import java.util.*;
public class Sort {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("fileToRead"));
Map<String, String> map=new TreeMap<String, String>();
String line="";
while((line=reader.readLine())!=null){
map.put(getField(line),line);
}
reader.close();
FileWriter writer = new FileWriter("fileToWrite");
for(String val : map.values()){
writer.write(val);
writer.write('\n');
}
writer.close();
}
private static String getField(String line) {
return line.split(" ")[0];//extract value you want to sort on
}
}
代码示例来源:origin: stackoverflow.com
f.delete();
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.append(encrypt(sb.toString().trim()));
} else {
bw.append(sb.toString().trim());
bw.flush();
bw.close();
BufferedReader br = new BufferedReader(new FileReader(f));
StringBuilder sb = new StringBuilder();
while (br.ready()) {
sb.append(br.readLine()).append(System.lineSeparator());
BufferedReader br = new BufferedReader(new FileReader(f));
StringBuilder sb = new StringBuilder();
while (br.ready()) {
代码示例来源:origin: apache/hive
private File subUserAndPassword(String parent, String filename) throws IOException {
File createFile = File.createTempFile("create-hive-user-" + schemaTool.getDbType(), ".sql");
BufferedWriter writer = new BufferedWriter(new FileWriter(createFile));
File proto = new File(parent, filename);
BufferedReader reader = new BufferedReader(new FileReader(proto));
reader.lines()
.map(s -> s.replace("_REPLACE_WITH_USER_", schemaTool.getHiveUser())
.replace("_REPLACE_WITH_PASSWD_", schemaTool.getHivePasswd())
.replace("_REPLACE_WITH_DB_", schemaTool.getHiveDb()))
.forEach(s -> {
try {
writer.write(s);
writer.newLine();
} catch (IOException e) {
throw new RuntimeException("Unable to write to tmp file ", e);
}
});
reader.close();
writer.close();
return createFile;
}
代码示例来源:origin: stackoverflow.com
BufferedReader in = new BufferedReader(new FileReader("file.in"));
FileWriter fstream = new FileWriter("file.out");
BufferedWriter out = new BufferedWriter(fstream);
String line = in.readLine(); // <-- read whole line
StringTokenizer tk = new StringTokenizer(line);
int a = Integer.parseInt(tk.nextToken()); // <-- read single word on line and parse to int
out.write(""+a);
out.flush();
代码示例来源:origin: stackoverflow.com
md = MessageDigest.getInstance("SHA1");
in = new BufferedReader(new FileReader(IN_FILE));
out = new BufferedWriter(new FileWriter(CHAR_FILE));
int count = 0;
for (int c = in.read(); c != -1; c = in.read()) {
md.update((byte) c);
out.write(c);
wrote++;
if (++count >= COUNT) {
md.update((byte) '\n');
out.write("\n");
wrote++;
count = 0;
内容来源于网络,如有侵权,请联系作者删除!