在Java中是否有一种方法可以在java中指定目录并逐个读取整个文件?
否则有没有办法在java中读取正则表达式文件?因此,如果文件夹中的所有文件都以gh001_12312 gh002_12312,gh003_12911,gh004_22222,gh005_xxxxx等开头
我试图使用DataOupPutStream.write()方法写入大于256的值.当我尝试使用DataInputStream.read()它读取相同的值时,它将返回0.因此,我使用DataOutputStream.writeInt()和DataInputStream.readInt()方法来编写和检索大于256的值,它工作正常.
请参考下面的代码片段我想知道编译器的行为,就像它在语句in.readInt()内部所做的那样while.
FileOutputStream fout = new FileOutputStream("T.txt");
BufferedOutputStream buffOut = new BufferedOutputStream(fout);
DataOutputStream out = new DataOutputStream(fout);
Integer output = 0;
out.writeInt(257);
out.writeInt(2);
out.writeInt(2123);
out.writeInt(223);
out.writeInt(2132);
out.close();
FileInputStream fin = new FileInputStream("T.txt");
DataInputStream in = new DataInputStream(fin);
while ((output = in.readInt()) > 0) {
System.out.println(output);
}
Run Code Online (Sandbox Code Playgroud)
运行此代码段时的输出是:
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readInt(Unknown Source)
at compress.DataIOStream.main(DataIOStream.java:34)
257
2
2123
223
2132
Run Code Online (Sandbox Code Playgroud)
但是当我在调试模式下运行时,我得到以下输出:
2123
223
2132
Exception in …Run Code Online (Sandbox Code Playgroud) 任何人都可以告诉我为什么在运行这个循环后我总是有这个奇怪的输出?这是一个线程问题还是什么?
for(int i=0;i<10;i++){
System.out.println("out: "+i);
System.err.println("err: "+(i+1));
}
Run Code Online (Sandbox Code Playgroud)
- >输出:
err: 1
out: 0
err: 2
err: 3
err: 4
out: 1
out: 2
out: 3
out: 4
err: 5
out: 5
err: 6
out: 6
err: 7
err: 8
out: 7
out: 8
err: 9
out: 9
err: 10
out: 10
Run Code Online (Sandbox Code Playgroud) 我有一个要求写入文件而不是System.out.println的请求并同时保存它,以便用户可以通过打开文件随时查看进度.
现在我正在使用以下代码
FileWriter fstream = null;
File file1 = new File(logFile);
fstream = new FileWriter(file1);
out = new BufferedWriter(fstream);
out.write(msg);
out.write("\n");
Run Code Online (Sandbox Code Playgroud)
但它不会将内容保存到文件中,除非我说Out.close();
仅供参考 - 1.这是一个Java EE应用程序代码.2.我不想使用Log4jLogger
有没有更简单的方法来做到这一点?有什么指针吗?
在我的在线计算机科学课上,我必须编写一个程序来确定太阳系中每个行星的表面引力.除了一个,我几乎已经完成了它的每个方面.我需要使用单独的方法将表面重力写入文件.这是我目前的方法:
public static void writeResultsToFile(double g) throws IOException{
PrintWriter outFile = new PrintWriter(new File("planetaryData.txt"));
outFile.printf("%.2f%n", g);
outFile.close();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我将它写入文件时,它将覆盖以前的值.我如何获得它包括所有值.如果有帮助,这是我的全部代码:
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
public class gravityV1 {
/**
* @param args
*/
public static double calculateSurfaceGravity(double d, double M){
double G = 6.67 * Math.pow(10, -11);
double r = d;
double g;
g = G * M/Math.pow(r/2*1000, 2);
return g;
}
public static void printResultsToScreen(String planetName, double diameterKm, double massKg, double g){
System.out.printf("%-15s%-17.0f%-17.2e%.2f%n", planetName, diameterKm, massKg, …Run Code Online (Sandbox Code Playgroud) 我需要在VM中写很多文件.我需要写300.000个文件,今天工作正常生成文件,但使用的时间是3~4小时完成工作.
如何实现这个并行线程?
Streams是惰性的,因此以下语句不会加载path内存引用的目录的整个子节点; 相反,它逐个加载它们,并且在每次调用之后forEach,引用的目录p都有资格进行垃圾收集,因此它的文件描述符也应该关闭:
Files.list(path).forEach(p ->
absoluteFileNameQueue.add(
p.toAbsolutePath().toString()
)
);
Run Code Online (Sandbox Code Playgroud)
基于这个假设,我实现了广度优先的文件遍历工具:
public class FileSystemTraverser {
public void traverse(String path) throws IOException {
traverse(Paths.get(path));
}
public void traverse(Path root) throws IOException {
final Queue<String> absoluteFileNameQueue = new ArrayDeque<>();
absoluteFileNameQueue.add(root.toAbsolutePath().toString());
int maxSize = 0;
int count = 0;
while (!absoluteFileNameQueue.isEmpty()) {
maxSize = max(maxSize, absoluteFileNameQueue.size());
count += 1;
Path path = Paths.get(absoluteFileNameQueue.poll());
if (Files.isDirectory(path)) {
Files.list(path).forEach(p ->
absoluteFileNameQueue.add(
p.toAbsolutePath().toString()
)
);
}
if (count % 10_000 …Run Code Online (Sandbox Code Playgroud) 声纳给出了一个错误,FileOutputStream应该将其关闭。我需要修改以下代码才能使用try-with-resources。我该怎么做呢?
public void archivingTheFile(String zipFile){
byte[] buffer = new byte[1024];
try{
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for(String file : this.fileList){
ZipEntry ze= new ZipEntry(file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
zos.close();
}catch(IOException ex){
LOGGER.error("Exception occurred while zipping file",ex);
}
}
Run Code Online (Sandbox Code Playgroud) 为什么Files.list抛出IOException而File.listFiles不是?
看一下Files.list(Java 8)的源代码,我更加好奇为什么没有抛出,UncheckedIOException因为它也被抛入迭代器中.
如果我用我替换File.listFiles-code Files.list现在需要处理一个我以前没有真正处理过的异常.毋庸置疑,大多数开发人员甚至不知道他们在那时需要处理什么;-)或者只是放一个// TODO和/或e.printStackTrace()那里.
这使得在Stream这里使用相当麻烦,因为你需要用一个try/catch或重新抛出异常来包围它,这在遗留代码中甚至是不可能的.
那么为什么要做出这个决定呢?
在复制大文件期间,此代码的运行速度越来越慢。难道我做错了什么?
InputStream ms2 = new BufferedInputStream(new FileInputStream("/home/fedd/Videos/homevid.mp4"));
OutputStream fos2 = new BufferedOutputStream(new FileOutputStream("testfile2.mp4", true));
try {
int byt;
int i = 0;
long time = System.currentTimeMillis();
while ((byt = ms2.read()) != -1) {
fos2.write(byt);
i++;
if (i > 100000) {
i = 0;
long took = System.currentTimeMillis() - time;
System.out.println("100000 bytes took " + took + " milliseconds which means " + (100000000 / took) + " bytes per second");
}
}
fos2.close();
ms2.close();
} catch (Exception e) {
throw …Run Code Online (Sandbox Code Playgroud) java ×10
java-io ×10
file-io ×3
nio ×2
binaryfiles ×1
concurrency ×1
java-8 ×1
java-stream ×1
printwriter ×1
sonarqube ×1
system.out ×1