java-io

PrintWriter add text to file

微笑、不失礼 提交于 2020-01-21 08:51:14
问题 In my online computer science class I have to write a program to determine the surface gravity on each planet in the solar system. I have gotten almost every aspect of it to work save one. I need to write the surface gravity to a file using a separate method. This is my current method: public static void writeResultsToFile(double g) throws IOException{ PrintWriter outFile = new PrintWriter(new File("planetaryData.txt")); outFile.printf("%.2f%n", g); outFile.close(); } My problem is that when

PrintWriter add text to file

两盒软妹~` 提交于 2020-01-21 08:51:06
问题 In my online computer science class I have to write a program to determine the surface gravity on each planet in the solar system. I have gotten almost every aspect of it to work save one. I need to write the surface gravity to a file using a separate method. This is my current method: public static void writeResultsToFile(double g) throws IOException{ PrintWriter outFile = new PrintWriter(new File("planetaryData.txt")); outFile.printf("%.2f%n", g); outFile.close(); } My problem is that when

Why cant a RandomAccessFile be casted to Inputstream?

丶灬走出姿态 提交于 2020-01-21 07:38:51
问题 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. 回答1: RandomAccessFile extends Object ,

isDirectory() returns true for a file

筅森魡賤 提交于 2020-01-15 05:54:18
问题 In my java program I copy a file and delete the new file. In my method removeFile() I check if it is a directory: String fileName = "G:/1310628353186Examples.csv"; File f = new File(fileName); if (f.isDirectory()) { System.out.println( "'" + fileName + "' is a directory" ); String[] files = f.list(); if (files != null && files.length > 0) throw new IllegalArgumentException("Delete: directory not empty: " + fileName); } Sometimes I get "'G:/1310628353186Examples.csv' is a directory" ,

Creating file and intermediate directories if does not exist

依然范特西╮ 提交于 2020-01-15 04:45:08
问题 I want to create a file if and only if that file does not exist. As an example file location is referring to "C:\user\Desktop\dir1\dir2\filename.txt" if (!file.exists()) { try { file.createNewFile(); } catch(IOException ioe) { ioe.printStackTrace(); return; } } Unfortunately the above code is failing, as the dir1 and dir2 does not exist. For my case Sometime the dir1 and dir2 may exist and sometime they may not exist. I do not want to overwrite the contents of these intermediate directories

Fastest way of processing Java IO using ASCII lines

别来无恙 提交于 2020-01-14 04:14:22
问题 I'm working with an ASCII input/output stream over a Socket and speed is critical. I've heard using the right Java technique really makes a difference. I have a textbook that says using Buffers is the best way, but also suggests chaining with DataInputStreamReader. For output I'm using a BufferedOutputStream with OutputStreamWriter which seems to be fine. But I am unsure on what to use for the input stream. I'm working on new lines, so would Scanner be of any use? Speed is critical, I need to

Serialization:java.io.StreamCorruptedException: invalid stream header: 0AACED00

强颜欢笑 提交于 2020-01-11 08:47:47
问题 I'm a student practicing my File IO skills and I am coming up against a problem with reading Objects from a file using ObjectInputStream. The code is consistently throwing an InvalidClassException and I can't find how the code is throwing it online or by trial and error. Here's my code: import java.io.*; import java.util.ArrayList; import java.util.List; public class ReadFromFile { String filename; List<Object> os; public ReadFromFile(String filename) { this.filename = filename; os = new

read and find string from text file

て烟熏妆下的殇ゞ 提交于 2020-01-06 21:48:06
问题 I am loading text file contents to GUI using this code: try { BufferedReader br = new BufferedReader(new FileReader ("text.txt")); String line; while ((line = br.readLine()) != null) { if (line.contains("TITLE")) { jTextField2.setText(line.substring(11, 59)); } } in.close(); } catch (Exception e) { } Then contents of text.txt file: JOURNAL journal name A12340001 TITLE Sound, mobility and landscapes of exhibition: radio-guided A12340002 tours at the Science Museum A12340003 AUTHOR authors name

How can I loop a sound without using 'clip' in java before writing it into a file

 ̄綄美尐妖づ 提交于 2020-01-05 04:54:09
问题 First of all thanks everyone for helping me with my previous questions. In the following code I am taking two frequencies alternatively and writing them into a .wav format, to run it in my Windows Media Player, for a specific time given by the user. What I want is to understand how to loop those frequencies to run alternatively for that specified time, like the Ambulance's siren, and in my program both frequencies are getting played just once, alternatively. For example, if I am specifying

java.io.File #listFiles() Returns null from Windows cmd, but not as Eclipse Project Run

橙三吉。 提交于 2020-01-05 04:05:21
问题 I have created this following method which I execute from a main thread public static LinkedList<File> checkFile(String path, LinkedList<File> result) Throws IOException, SecurityException{ File root = new File(path); File[] list = root.listFiles(); if (list == null) return result; for (File f : list) { if (f.isDirectory()) { checkFile(f.getAbsolutePath(), result); } else { result.add(f.getAbsoluteFile()); } } return result; } As long as it looks into the same location as the file itself, it