java-io

How to check whether an OutputStream is closed

耗尽温柔 提交于 2019-11-28 10:42:15
Is there anyway to check whether an OutputStream is closed without attempting to write to it and catching the IOException ? For example, consider the following contrived method: public boolean isStreamClosed(OutputStream out){ if( /* stream isn't closed */){ return true; }else{ return false; } } What could you replace /* stream isn't closed */ with? The underlying stream may not know it its closed until you attempt to write to it (e.g. if the other end of a socket closes it) The simplest approach is to use it and handle what happens if it closed then, rather than testing it first as well. No

ClassCastException when Appending Object OutputStream

本小妞迷上赌 提交于 2019-11-28 10:21:53
I have been trying to do a little project that needs an appendable ObjectOutputStream. I have gone through a couple of solutions and i found this It seemed to solve my problem at first. But on further development of my project i started getting unexpected exceptions. The following are my classes. 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 {

How to list only N files in directory using java

只愿长相守 提交于 2019-11-28 09:02:17
问题 If I have a directory contains a lot of files (about 1000 file). Some of these files named .processed and other not. How can I list only 10 unprocessed files. I am using this code to filter the processed file. File[] inputFileList = inputDirectory.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return !pathname.getName().endsWith(".processed"); } }); But if the number of un-processed files is huge, this may lead to memory error. so I need to read a limited number

Best way to Pipe InputStream to OutputStream [duplicate]

痞子三分冷 提交于 2019-11-28 08:06:50
This question already has an answer here: Easy way to write contents of a Java InputStream to an OutputStream 22 answers I was to trying to find the best way to pipe the InputStream to OutputStream. I don't have an option to use any other libraries like Apache IO. Here is the snippet and output. import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.channels.FileChannel; public class Pipe { public static void main(String[] args) throws Exception { for(PipeTestCase testCase : testCases

Java File.exists() versus File.isFile()

倖福魔咒の 提交于 2019-11-28 04:21:09
问题 I'm unable to think of a realistic use case for the method java.io.File.exists() or its equivalent in Java 7 java.nio.file.Files.exists(Path). It seems that isFile() or isDirectory() would be preferable in all cases (or canRead() , canWrite() , etc.). For example, in How do I check if a file exists in Java?, the accepted answer seems silly, as the second answer points out. Can anyone give an example where it's useful to know that a thing exists, without knowing whether the thing is a file or

Is PrintWriter buffered?

喜欢而已 提交于 2019-11-28 00:45:53
问题 I know that the PrintWriter is really good if we want to write formatted data, and I also know the use of BufferedWriter to improve IO performance. But I tried something like this, PrintWriter pw = new PrintWriter(System.out); pw.println("Statement 1"); pw.println("Statement 2"); //pw.flush(); I observed that when the flush method is commented there is no output, but when I uncomment it, I get the desired output. This is only possible if the PrintWriter is buffered. If so, then what is the

Java read file with whitespace in its path

Deadly 提交于 2019-11-28 00:33:20
问题 I am trying to open files with FileInputStream that have whitespaces in their names. For example: String fileName = "This is my file.txt"; String path = "/home/myUsername/folder/"; String filePath = path + filename; f = new BufferedInputStream(new FileInputStream(filePath)); The result is that a FileNotFoundException is being thrown. I tried to hardcode the filePath to "/home/myUserName/folder/This\\ is\\ my\\ file.txt" just to see if i should escape whitespace characters and it did not seem

Basics - reading/writing remote files using Java

戏子无情 提交于 2019-11-27 20:52:28
I started with requirement of reading and writing files in from/in a directory on a remote Ubuntu machine. First, I wrote a Java program that could read,write files from a shared folder on a remote Windows machine i.e on a LAN. Here, something like this works on my(local) Windows machine : File inputFile = new File( "\\172.17.89.76\EBook PDF");/*ignore the syntax errors, the loc is just for the idea*/ Now when I consider a remote Ubuntu machine, obviously I cannot do something like this as the machine is not on the LAN( I'm not sure if that can be done even if it is on the LAN !). Hence, I

Java. How to append text to top of file.txt [duplicate]

别说谁变了你拦得住时间么 提交于 2019-11-27 20:30:28
This question already has an answer here: Writing in the beginning of a text file Java 5 answers I need to add text to beggining of text file via Java. For example I have test.txt file with data: Peter John Alice I need to add(to top of file): Jennifer It should be: Jennifer Peter John Alice I have part of code, but It append data to end of file, I need to make It that added text to top of file: public static void irasymas(String irasymai){ try { File file = new File("src/lt/test.txt"); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);

implements Closeable or implements AutoCloseable

与世无争的帅哥 提交于 2019-11-27 19:45:40
问题 I'm in the process of learning Java and I cannot find any good explanation on the implements Closeable and the implements AutoCloseable interfaces. When I implemented an interface Closeable , my Eclipse IDE created a method public void close() throws IOException . I can close the stream using pw.close(); without the interface. But, I cannot understand how I can implement the close() method using the interface. And, what is the purpose of this interface? Also I would like to know: how can I