java-io

Java, the fastest class to read from a txt file [closed]

别来无恙 提交于 2019-12-22 05:29:13
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I have to read txt files in my program. Im currently using FileReader and BufferedReader. I tried to use Scanner but its slower than

How do I read and write to a file using threads in java?

天大地大妈咪最大 提交于 2019-12-21 21:13:03
问题 I'm writing an application where I need to read blocks in from a single file, each block is roughly 512 bytes. I am also needing to write blocks simultaneously. One of the ideas I had was BlockReader implements Runnable and BlockWriter implements Runnable and BlockManager manages both the reader and writer. The problem that I am seeing with most examples that I have found was locking problems and potential deadlock situations. Any ideas how to implement this? 回答1: I would recommend the book

How do I read Windows NTFS's Alternate Data Stream using Java's IO?

怎甘沉沦 提交于 2019-12-21 20:57:21
问题 I'm trying to have my Java application read all the data in a given path. So files, directories, metadata etc. This also includes one weird thing NTFS has called Alternate Data Stream (ADS). Apparently it's like a second layer of data in a directory or file. You can open the command prompt and create a file in the ADS using ':', for example: C:\ADSTest> echo test>:ads.txt So, C:\ADSTest> notepad :ads.txt Should open a notepad that contains the string "test" in it. However, if you did: C:

Return File From Resteasy Server

一世执手 提交于 2019-12-21 04:13:38
问题 Hi, I wanted to return a file from a resteasy server. For this purpose, I have a link at the client side which is calling a rest service with ajax. I want to return the file in the rest service. I tried these two blocks of code, but both didn't work as I wanted them to. @POST @Path("/exportContacts") public Response exportContacts(@Context HttpServletRequest request, @QueryParam("alt") String alt) throws IOException { String sb = "Sedat BaSAR"; byte[] outputByte = sb.getBytes(); return

java.nio.file.Files.delete(Path path) - occasional failure to recursively delete directory using SimpleFileVisitor

扶醉桌前 提交于 2019-12-21 03:32:28
问题 Trying to troubleshoot an occasional java.nio.file.DirectoryNotEmptyException in a recursive delete method taken from Delete directories recursively in Java Code (credit to @TrevorRobinson) : static void removeRecursive(Path path) throws IOException { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { final Logger logger = LoggerFactory.getLogger(this.getClass()); @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { logger.warn("Deleting

Java: accessing a List of Strings as an InputStream

那年仲夏 提交于 2019-12-20 11:59:29
问题 Is there any way InputStream wrapping a list of UTF-8 String ? I'd like to do something like: InputStream in = new XyzInputStream( List<String> lines ) 回答1: You can concatenate all the lines together to create a String then convert it to a byte array using String#getBytes and pass it into ByteArrayInputStream. However this is not the most efficient way of doing it. 回答2: You can read from a ByteArrayOutputStream and you can create your source byte[] array using a ByteArrayInputStream . So

java 6 IO - wrapped streams closing [closed]

不羁岁月 提交于 2019-12-20 07:48:40
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Consider : public static void read(String filename) throws IOException { String charsetName = "UTF-8"; InputStream file = new

Java - Skip first line while using try with resources

一笑奈何 提交于 2019-12-20 07:40:04
问题 I need to skip the very first line of what is found in the file. My code: List<String> readStuff() { String pathName = "D:/java/eclipse/someStuff.txt"; List<String> list = new ArrayList<>(); try (Stream<String> lines = Files.lines(Paths.get(pathName))) { list = lines.collect(Collectors.toList()); } catch (IOException e) { System.out.println("Failed to load file."); } return list; } 回答1: You can just invoke skip(n) to skip the first n-th element from the stream. In this case, using skip(1)

Reading single InputStream from multiple methods

亡梦爱人 提交于 2019-12-20 07:09:06
问题 I have initialized an InputStream in a single method in a class and passing it to next method for processing. The InputStream essentially encapsulates CSV file for processing. Another method calls 2 different methods passing in same InputStream one for retrieving headers and another for processing contents. The structure looks something as given below: main() { FileInputStream fis = new FileInputStream("FileName.CSV"); BufferedInputStream bis = new BufferedInputStream(fis); InputStreamReader

Sonar: How to use try-with-resources to close FileOutputStream

廉价感情. 提交于 2019-12-20 02:44:09
问题 Sonar is giving an error that this FileOutputStream should be closed. I need to modify the following code to use try-with-resources . How do I do this? 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