Tag: file io

Java:将文本文件输出到Console

我正在尝试使用Java将文本文件输出到控制台。 我想知道最有效的方法是什么? 我已经研究了几种方法,但是很难分辨哪种方法影响最小的解决方案。 将文本文件输出到控制台将涉及读取文件中的每一行,然后将其写入控制台。 是否更好使用: 带有FileReader的缓冲读取器,读取行并执行一堆system.out.println调用? BufferedReader in = new BufferedReader(new FileReader(“C:\\logs\\”)); while (in.readLine() != null) { System.out.println(blah blah blah); } in.close(); 扫描程序读取文件中的每一行并执行system.print调用? while (scanner.hasNextLine()) { System.out.println(blah blah blah); } 谢谢。

Java编译器抱怨未报告的IOException

我正在尝试编写一个列出目录中所有非隐藏文件的方法。 但是,当我添加条件!Files.isHidden(filePath)我的代码将无法编译,并且编译器返回以下错误: java.lang.RuntimeException: Uncompilable source code – unreported exception java.io.IOException; must be caught or declared to be thrown 我试图捕获IOException ,但编译器仍然拒绝编译我的代码。 有什么明显的东西让我失踪吗? 代码如下。 try { Files.walk(Paths.get(root)).forEach(filePath -> { if (Files.isRegularFile(filePath) && !Files.isHidden(filePath)) { System.out.println(filePath); } }); } catch(IOException ex) { ex.printStackTrace(); } catch(Exception ex) { ex.printStackTrace(); }

使用DataInputStream从文件读取非常慢

我有一个包含大量数字的文件。 我曾尝试使用以下代码从文件中读取它,但它是超级慢任何人都可以帮助减少时间? 以下是我的代码以非常慢的方式阅读它: import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.*; public class FileInput { public static void main(String[] args) { Scanner scan1 = new Scanner(System.in); String filename = scan1.nextLine(); File file = new File(filename); FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; try { […]

放置.txt文件的位置,以便java servlet可以在运行时读取它

我正在使用eclipse制作动态网页。 它是一个依赖servlet传递/检索数据的简单程序。 我的一个servlet必须打开.txt文件来读取其内容并将其发送到客户端。 但是我收到了FileNotFoundexception。 我知道它,因为我不知道如何/在哪里放置txt文件,以便servlet可以在运行时找到该文件。 我正在研究日食。 你能提供一些提示吗? 谢谢

将JTextArea内容写入文件

我在Java Swing中有一个JTextArea和一个Submit按钮。 需要将textarea的内容写入包含换行符的文件中。 我得到了输出,它被写为文件中的一个字符串。 try { BufferedWriter fileOut = new BufferedWriter(new FileWriter(“filename.txt”)); String myString1 =jTextArea1.getText(); String myString2 = myString1.replace(“\r”, “\n”); System.out.println(myString2); fileOut.write(myString2); fileOut.close(); } catch (IOException ioe) { ioe.printStackTrace(); } 请给我一些建议

Java MemoryMapping大文件

MappedByteBuffer对2GIG的Java限制使得用于映射大文件变得棘手。 通常推荐的方法是使用MappedByteBuffer数组并通过以下方式对其进行索引: long PAGE_SIZE = Integer.MAX_VALUE; MappedByteBuffer[] buffers; private int getPage(long offset) { return (int) (offset / PAGE_SIZE) } private int getIndex(long offset) { return (int) (offset % PAGE_SIZE); } public byte get(long offset) { return buffers[getPage(offset)].get(getIndex(offset)); } 这可能适用于单个字节,但如果要处理更大且需要跨越边界的读/写(getLong()或get(byte [])),则需要重写大量代码。 问题是:对于这些场景,你最好的做法是什么,你知道任何可以重复使用的工作解决方案/代码而不重新发明轮子吗?

番石榴,Files.readLines()和空白区域

我有以下代码使用Guava的Files.readLines()方法: List strings = Lists.newArrayList(); final File file = new File(filePath); if (file.isFile()) { try { strings= Files.readLines(file, Charsets.UTF_8); } catch (IOException ioe) {} } else { // File does not exist } 一旦我有了我的String列表,我想测试一下,看看我正在查看的当前字符串是否在文件中。 String myString = “foo”; if (strings.contains(myString)) { // Do something } 但是,我想使容忍原始文件的代码包含前导或尾随空格(即” foo ” )。 实现这一目标的最优雅方式是什么?

PrintWriter和FileWriter类之间的区别

try{ File file = new File(“write.txt”); FileWriter writer = new FileWriter(file); PrintWriter printWriter = new PrintWriter(writer); printWriter.println(“pqr”); printWriter.println(“jkl”); printWriter.close(); PrintWriter printWriter = new PrintWriter(file); printWriter.println(“abc”); printWriter.println(“xyz”); printWriter.close(); } 我不明白这两种方式有什么区别。 在哪种情况下我应该使用printWriter和fileWriter。

用Java读取和写入同一个文件

我想从criteria.txt文件中读取,以标记化并在同一文件的末尾附加标记。 该程序抛出exception: No file found! 我不知道我的错误在哪里。 任何建议都会对我有所帮助。 先感谢您! 这是我的代码: import java.io.*; import java.util.StringTokenizer; public class Test { private FileReader fr; private BufferedReader br; private FileWriter fw; private BufferedWriter bw; private StringTokenizer strtok; private String s; //constructor public Test() { try { fw=new FileWriter(“criteria.txt”, true); bw=new BufferedWriter(fw); try { fr=new FileReader(“criteria.txt”); br=new BufferedReader(fr); while((s=br.readLine())!=null) { strtok=new […]

使用Java在两个或多个套接字之间执行零拷贝数据传输

有没有人知道在两个或多个套接字之间执行零拷贝数据传输的任何好的java库/ API包? 我知道Java的NIO API可以分别使用java.nio.channels.FileChannel.transferTo和java.nio.channels.FileChannel.transferFrom方法从磁盘到套接字执行零拷贝数据传输,反之亦然。 但是,似乎没有支持Java套接字到套接字零拷贝传输。 此外,任何可以执行系统调用拼接的java库/ API(可以将数据从文件描述符传输到管道,反之亦然)将是一个优势,最好是在Linux平台上。 谢谢你的回复。 此外,我已经阅读了大多数以前关于零拷贝的博客以及其他信息性网站,例如http://www.ibm.com/developerworks/library/j-zerocopy/ ; 但是,似乎上述问题尚未得到解决。