问候,我从数据库中获取大量记录并写入文件.我想知道编写大文件的最佳方法是什么.(1Gb - 10Gb).
目前我正在使用BufferedWriter
BufferedWriter mbrWriter=new BufferedWriter(new FileWriter(memberCSV));
while(done){
//do writings
}
mbrWriter.close();
Run Code Online (Sandbox Code Playgroud) 我一直在尝试做一个需要可附加的ObjectOutputStream的小项目.我已经经历了几个解决方案,我发现这一点似乎首先解决了我的问题.但是在我的项目的进一步发展中,我开始遇到意外的异常.以下是我的课程.
public class PPAccount implements Serializable
{
private Profile profile;
private String email;
private float accountBal;
private boolean isActivated;
private String activationCode;
private ArrayList<Transaction> transactions;
//a few functions
}
public class PPRestrictedAccount extends PPAccount {
private String parentEmail;
private float withdrawLimit;
//a few functions
}
public class PPBusinessAccount extends PPAccount {
private ArrayList <PPRestrictedAccount> accountOperators;
//a few functions
}
public class PPStudentAccount extends PPAccount {
private String parentEmail;
//a few functions
}
Run Code Online (Sandbox Code Playgroud)
我观察到的是,使用这个我已经覆盖了ObjectOutputStream并在我将对象附加到文件时使用它.但是如果我写的话会发生什么:
PPBusinessAccount首先,重复任意次数...然后写PPAccount …
我必须在我的程序中读取txt文件.我目前正在使用FileReader和BufferedReader.我尝试使用Scanner,但速度比FileReader和BufferedReader慢.有没有可以更快读取文件的类?它必须用Java语言编写.
我需要从文本文件中读取所有单词(由空格分割的字符串)
我想为我的报告创建一个HTML文件.可以使用创建报告中的内容BufferedWriter#write(String)
File f = new File("source.htm");
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write("Content");
Run Code Online (Sandbox Code Playgroud)
或者通过使用 DataOutputStream#writeBytes(String)
File f = new File("source.htm");
DataOutputStream dosReport = new DataOutputStream(new FileOutputStream(f));
dosReport.wrtiteBytes("Content");
Run Code Online (Sandbox Code Playgroud)
其中一个比另一个好吗?为什么会这样?
所以,基本上我想从base64字符串内容创建一个临时文件.现在,我正在使用本机java-io函数.但我想用scala的rapture-io库来实现相同的结果.
所以我的问题是,是否有可能通过狂喜来实现这一点,如果是这样,怎么样?
我已经阅读了文档,但不够具体:
https://github.com/propensive/rapture-io/blob/master/doc/introduction.md
这是我的实际代码:
import org.apache.commons.codec.binary.Base64
import java.io.FileOutputStream
import java.io.File
val data: String = base64StringContent //Base64 String content of the file.
val fileName = myFileName
val fileExt = myFileExt
//It does write the file in my temp folder.
val file: File = File.createTempFile(fileName, fileExt)
val fileByteArray: Array[Byte] = Base64.decodeBase64(data)
val fileOutFile: FileOutputStream = new FileOutputStream(file)
fileOutFile.write(fileByteArray)
fileOutFile.close()
file.deleteOnExit()
file
Run Code Online (Sandbox Code Playgroud) 我知道PrintWriter如果我们想要编写格式化数据,那真的很好,而且我也知道使用它BufferedWriter来提高IO性能.
但我试过这样的事,
PrintWriter pw = new PrintWriter(System.out);
pw.println("Statement 1");
pw.println("Statement 2");
//pw.flush();
Run Code Online (Sandbox Code Playgroud)
我观察到当注释flush方法时没有输出,但是当我取消注释它时,我得到了所需的输出.
只有在PrintWriter被缓冲时才可以这样做.如果是这样,那么使用BufferedWriter包装PrintWriter然后编写它有什么意义呢?
虽然javadoc没有提到PrintWriter被缓冲的任何地方,但似乎如此.
我试图/proc/net/xt_qtaguid/stats在Android 6中阅读.
使用cat命令,我得到这个:
2 a0 0 0 123456 311 48329 737 48 1
3 b0 0 0 0 0 0 0 0
4 c0 123456 311 48329 737 48 1
5 d0 111111 111 22222 222 33 1
Run Code Online (Sandbox Code Playgroud)
我的java代码尝试逐行读取文件:
File sysDataFile = new File(PATH_TO_FILE);
BufferedReader bufReader = null;
FileReader fileReader = null;
try {
fileReader = new FileReader(sysDataFile);
bufReader = new BufferedReader(fileReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
// print to …Run Code Online (Sandbox Code Playgroud) 我正在编写程序部分只是为了将文件从源文件复制到目标文件.代码也可以正常运行,但是如果存在文件大文件,则在目标文件达到4.3 GB的大小之后,复制过程将结束,但有异常.例外情况是"文件大到"它看起来像:
java.io.IOException: Die Datei ist zu groß
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.FileDispatcherImpl.write(FileDispatcherImpl.java:60)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:211)
at java.nio.channels.Channels.writeFullyImpl(Channels.java:78)
at java.nio.channels.Channels.writeFully(Channels.java:101)
at java.nio.channels.Channels.access$000(Channels.java:61)
at java.nio.channels.Channels$1.write(Channels.java:174)
at java.nio.file.Files.copy(Files.java:2909)
at java.nio.file.Files.copy(Files.java:3069)
at sample.Controller.copyStream(Controller.java:318)
Run Code Online (Sandbox Code Playgroud)
产生的方法如下:
private void copyStream(File src, File dest){
try {
FileInputStream fis = new FileInputStream(src);
OutputStream newFos = java.nio.file.Files.newOutputStream(dest.toPath(),StandardOpenOption.WRITE);
Files.copy(src.toPath(),newFos);
newFos.flush();
newFos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试使用java.io Fileoutputstream并以kbyte方式写入,但发生的情况相同.如何复制或创建大于4.3 GB的文件?是否可能用java以外的其他语言?这个程序我运行在Linux(Ubuntu LTS 16.04)上.
提前致谢.
编辑:
非常感谢大家的帮助.正如你所说,文件系统是问题所在.在我将文件系统格式化为exfat后,它工作正常.
我想知道是否有任何方法可以更新符号链接目标而不删除旧的符号链接。
我有两段代码,它们同时发生。第一部分是每小时下载一些数据并将其存储在 中/some/directory,并且它还将创建/most/recent/data指向新下载的数据的符号链接点。
第二部分将从 symlink 读取最新数据/most/recent/data。
下面是我当前创建符号链接的实现。但问题是,如果我们删除原来的符号链接,同时读取它,就会出现问题。有没有办法在不删除旧符号链接的情况下更新符号链接目标?
private void createSymLink(String symLinkPath, Path basePath) {
try {
Files.deleteIfExists(Paths.get(symLinkPath));
Files.createSymbolicLink(Paths.get(symLinkPath), basePath);
} catch (IOException e) {
}
}
Run Code Online (Sandbox Code Playgroud)
我想我刚刚找到了一些可能的解决方案。创建一个 tmp 符号链接,然后将其移至旧符号链接。因为移动是一个原子操作,所以符号链接更新也将是原子的。
private void createSymLink(String symLinkPath, Path basePath) {
try {
String tmpLink = "/tmp/tmpdir";
Files.createSymbolicLink(Paths.get(tmpLink), basePath);
Files.move(Paths.get(tmpLink), Paths.get(symLinkPath), ATOMIC_MOVE);
} catch (IOException e) {
}
}
Run Code Online (Sandbox Code Playgroud) java-io ×10
java ×9
file-io ×2
android ×1
file ×1
filereader ×1
io ×1
linux ×1
printwriter ×1
rapture.io ×1
scala ×1
string ×1
symlink ×1
ubuntu-16.04 ×1