java-io

Why cant a RandomAccessFile be casted to Inputstream?

烂漫一生 提交于 2019-12-01 06:33:45
I get compilation error when I do this cast: RandomAccessFile raf = new RandomAccessFile(...) InputStream is = (InputStream)raf; RandomAccessFile is supposed to subclass InputStream although not directly. From docs: RandomAccessFile implements DataInput which inturn DataInputstream & InputStream Why is this invalid? Also appreciate your input on what would be the right way to use the RandomAccessFile as InputStream ? I am thinking of wrapper approach. RandomAccessFile extends Object , and does not extend InputStream . If you want get an InputStream from a RandomAccessFile I think implementing

JAVA : read and write a file together

三世轮回 提交于 2019-12-01 04:36:42
I am trying to read a java file and modify it simultaneously. This is what I need to do : My file is of the format : aaa bbb aaa ccc ddd ddd I need to read through the file and get the count of the # of occurrences and modify the duplicates to get the following file: aaa - 2 bbb - 1 ccc - 1 ddd - 2 I tried using the RandomAccessFile to do this, but couldn't do it. Can somebody help me out with the code for this one? It's far easier if you don't do two things at the same time. The best way is to run through the entire file, count all the occurrences of each string in a hash and then write out

JAVA : read and write a file together

試著忘記壹切 提交于 2019-12-01 02:09:34
问题 I am trying to read a java file and modify it simultaneously. This is what I need to do : My file is of the format : aaa bbb aaa ccc ddd ddd I need to read through the file and get the count of the # of occurrences and modify the duplicates to get the following file: aaa - 2 bbb - 1 ccc - 1 ddd - 2 I tried using the RandomAccessFile to do this, but couldn't do it. Can somebody help me out with the code for this one? 回答1: It's far easier if you don't do two things at the same time. The best

BufferedReader, detecting if there is text left to read

爷,独闯天下 提交于 2019-11-30 21:40:18
I'm running a thread and everytime it runs, It should be checking to see if there is a new line to read from the BufferedReader although, it gets stuck waiting for a line to exist, thus halting the entire code. if((inputLine = bufferedReader.readLine()) != null){ System.out.println(inputLine); JOptionPane.showMessageDialog(null, inputLine); } Is there a way to better check if there is text in a BufferedReader to be read? No, there's no easy way to do that. BufferedReader has a ready call, but that only applies to the read calls, not the readLine call. If you really want a readLine that's

How to pipe InputStream to ProcessBuilder

眉间皱痕 提交于 2019-11-30 13:47:53
Please move down to the 2nd update. I didn't want to change the previous context of this question. I'm using wkhtmltoimage from a Java app. The standard way of using it is - path-to-exe http://url.com/ image.png . According to their docs, if we write a - instead of an input URL, the input shifts to STDIN. I'm starting the process using ProcessBuilder - ProcessBuilder pb = new ProcessBuilder(exe_path, " - ", image_save_path); Process process = pb.start(); Now I'm unable to figure out how to pipe an input stream to this process. I have a template file read into a DataInputStream , and I'm

Sockets: BufferedReader readLine() blocks

人走茶凉 提交于 2019-11-30 12:24:11
I am using BufferedReader.readLine() method to read a response from a remote server (which is written in C and I have no access to source code). BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; while((line = br.readLine())!=null){ [...] } But it always blocks at the last line until it times out. So I used the following code: int b; while(true){ b = in.read; [...] } and I found out that the last byte read has an integer value of 13, which I think it is a carriage return, right? So why the readLine method blocks? How does the server usually signal an end of stream

Most efficient way to read text file and dump content into JTextArea

懵懂的女人 提交于 2019-11-30 09:37:44
问题 I am curious what the most efficient way is to read a text file (do not worry about size, it is reasonably small so java.io is fine) and then dump its contents into a JTextArea for display. E.g. can I somehow consume the entire file in a single string and then use JTextArea.setText to display it or should I read line by line or byte arrays and populate them into a StringBuffer and then set the text area to that? Thanks 回答1: You can use JTextComponent.read(Reader, Object) and pass it a

How to find sub-directories in a directory/folder?

喜夏-厌秋 提交于 2019-11-30 07:50:59
问题 I'm looking for a way to get all the names of directories in a given directory, but not files. For example, let's say I have a folder called Parent , and inside that I have 3 folders: Child1 Child2 and Child3 . I want to get the names of the folders, but don't care about the contents, or the names of subfolders inside Child1, Child2, etc. Is there a simple way to do this? 回答1: You can use String[] directories = file.list() to list all file names, then use loop to check each sub-files and use

Delete Files with same Prefix String using Java

不打扰是莪最后的温柔 提交于 2019-11-30 07:46:32
问题 I have around 500 text files inside a directory with a same prefix in their filename say dailyReport_ . The latter part of the file is the date of the file. (For eg. dailyReport_08262011.txt , dailyReport_08232011.txt ) I want to delete these files using a Java procedure (I could go for a shell script and add it a job in the crontab but the application is meant to used by laymen). I can delete one single file using something like this try{ File f=new File("dailyReport_08232011.txt"); f.delete

hadoop java.io.IOException: while running namenode -format

回眸只為那壹抹淺笑 提交于 2019-11-30 06:59:05
I ran namenode -format.This is my output. I tried changing the file permissions chmod 777 hadoop. I believe this line is the error ERROR namenode.NameNode: java.io.IOException: Cannot create directory /your/path/to/hadoop/tmp/dir/hadoop-hadoop/dfs/name/current adoop@alexander-desktop:/usr/local/hadoop/bin$ ./hadoop namenode -format 12/07/03 17:03:56 INFO namenode.NameNode: STARTUP_MSG: /************************************************************ STARTUP_MSG: Starting NameNode STARTUP_MSG: host = alexander-desktop/127.0.1.1 STARTUP_MSG: args = [-format] STARTUP_MSG: version = 0.20.2 STARTUP