Tag: io

为什么FileChannel.map会占用Integer.MAX_VALUE数据?

我在使用FileChannel.map时遇到exception Exception in thread “main” java.lang.IllegalArgumentException: Size exceeds Integer.MAX_VALUE at sun.nio.ch.FileChannelImpl.map(Unknown Source) at niotest.NioTest.readUsingNio(NioTest.java:38) at niotest.NioTest.main(NioTest.java:64) 快速查看OpenJdk实现显示FileChannelImpl中的方法map(..)将long类型的size作为输入。 但是在体内,它将它与Integer.MAX_VALUE进行比较,如果它大于那,则抛出错误。 为什么将long尺寸作为输入但是将其限制为最大integer长度? 有谁知道这个实现背后的具体原因? 还是某种虫子? 源URL – http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/nio/ch/FileChannelImpl.java 我在64位Windows-2k8上使用64位JRE运行此程序

Windows中的文件路径的Windows转义序列问题

我需要使用Windows文件路径对文件执行一些操作,但我得到无效的转义序列错误。 File f = new File(“C:\test”); 系统只接受“\\”或“/”,但如果我从Windows复制文件路径,则为“\”。 我怎么能解决这个问题

在java中流式传输后不保存对象属性更改

更新:好的,所以我将部分代码变灰并找到导致问题的原因。 我在这里添加了3行代码,注释“这是导致问题的添加代码”。 但我仍然不明白为什么它会影响结果。 我正在开发一个客户端 – 服务器应用程序,它通过ObjectOutputStream和ObjectInputStream发送数据对象。 我注意到一些奇怪的东西让我觉得我可能不完全理解对象引用。 在客户端,我有一个创建并返回User对象的方法: private static User createNewUser() { User newUser = new User(); newUser.name = “Jim”; newUser.phone = “054-6885644”; .. return newUser; } 我使用此方法创建一个User对象,更改其中一个属性并将其发送到服务器: User user = createNewUser(); out.writeObject(user); // this is the added code that causes the problem out.flush(); // this is the added code that causes the problem […]

为什么\ n和(String)System.getProperty(“line.separator”); 行为不同?

我的程序接受一个文本文件的输入,其中每个单词都用换行符分隔,我的程序接受并处理数据,然后我需要在保持控制台输出的同时输出到新文件。 现在我想知道为什么当我将“\ n”附加到我的stringBuilder时,它将它打印出来,因为它在控制台中有一个新行,但是在文件输出中,它不会将它作为一个新行而只是将所有单词放在一行中。 当我使用newLine时,它只在我的控制台输出和输出文件中给出一个新行。 这是为什么? (String)System.getProperty(“line.separator”)做了什么导致这个? String newLine = (String)System.getProperty(“line.separator”); try{ BufferedReader fileIn = new BufferedReader(new FileReader(fileName)); stringBuilder.append(newLine); while((s = fileIn.readLine()) != null){ stringBuilder.append(s); stringBuilder.append(newLine);//using newLine, } String a = stringBuilder.toString(); if(s== null){ fileIn.close(); }

Java:除非手动刷新,否则无法从Process获取stdout数据

我正在用Java编写命令行程序的终端包装器,并使用ProcessBuilder生成子进程。 要将键e.getKeyChar()子e.getKeyChar() ,我只需将GUI中的e.getKeyChar()直接写入e.getKeyChar()给出的OutputStream 。 为了从子进程接收输出,我基本上有一个从子进程的stdout读取的while循环: while ((b = br.read()) != -1) { System.out.println(“Read “+b); bb[0] = (byte) b; // call an event listener with the read byte listener.dataReceived(bb); } 只有当我立即冲洗两端的输出时,这才有效。 也就是说,我必须刷新每个用户输入,并且子进程必须刷新自己的stdout才能发生这些事情。 否则, read()阻塞,等待数据,这些数据从未实际发送过(子进程’stdout只是保持缓冲)。 我怎样才能进行I / O操作? 示例终端子进程: #include int main() { char c; while((c = getchar()) != -1) { printf(“Got: %d\n”, c); // doesn’t work […]

使用JDK 5 api从zip文件中提取时保持文件权限

我正在使用java.util.Zip和java.util.ZipEntry成功地将zip文件的内容添加到磁盘。 我想在* nix文件系统上提取时保持设置的文件权限。 任何人都可以指出我这样做的“正确”方法吗?

在java中打开临时文件

我正在写字符串到临时文件( temp.txt ),我希望在我关闭该文件后打开我应该删除的awt窗口按钮后打开该文件(打开该文件后),我该怎么做? 这是我用来在Java中创建临时文件的代码: File temp = File.createTempFile(“temp”,”.txt”); FileWriter fileoutput = new FileWriter(temp); Bufferedwriter buffout = new BufferedWriter(fileoutput);

Java使用inputstream从外部程序读取标准输出

我正在尝试开发一个读取外部程序标准输出的类(使用Process,Runtime.getRuntime()。exec(cmdLine,env,dir)的实例)。 程序在过程中接受用户输入,并且在给出有效输入之前不会继续; 这似乎导致我尝试读取其输出的方式出现问题: egm.execute(); // run external the program with specified arguments BufferedInputStream stdout = new BufferedInputStream(egm.getInputStream()); BufferedInputStream stderr = new BufferedInputStream(egm.getErrorStream()); BufferedOutputStream stdin = new BufferedOutputStream(egm.getOutputStream()); int c; //standard output input stream int e; //standadr error input stream while((c=stdout.read()) != -1) //<– the Java class stops here, waiting for input? { egm.processStdOutStream((char)c); } while((e=stderr.read()) […]

如何在Java中合并两个输入流?

在Java中有两个InputStream,有没有办法合并它们,所以你用一个InputStream结束,它给你两个流的输出? 怎么样?

将大型ResultSet写入文件

我试着把一个大的ResulSet(~1mm行)写到一个文件中。 在Java 1.6中有一个首选/有效的方法吗?