filewriter

How to replace a specific line in a file using Java?

这一生的挚爱 提交于 2019-12-22 13:05:12
问题 How do I write over a specific line in a text file using FileWriter and PrintWriter? I don't want to have to make a new file every time. Edit: Can I just cycle through the file, get the length of the String at the indicated line number, and then use that length to backspace once I get to that line (to delete the String), and write in the new data? public static void setVariable(int lineNumber, String data) { try { // Creates FileWriter. Append is on. FileWriter fw = new FileWriter("data.txt",

Copying the Contents of One text file to Another in Java

谁说我不能喝 提交于 2019-12-21 04:29:17
问题 I am trying to copy the contents of one text file ("1.txt") which contains 2-3 integer numbers (ex: 1 2 3) to another text file ("2.txt") but I am getting the following error upon compilation import java.io.*; class FileDemo { public static void main(String args[]) { try { FileReader fr=new FileReader("1.txt"); FileWriter fw=new FileWriter("2.txt"); int c=fr.read(); while(c!=-1) { fw.write(c); } } catch(IOException e) { System.out.println(e); } finally() { fr.close(); fw.close(); } } }

(java) Writing in file little endian

╄→гoц情女王★ 提交于 2019-12-20 17:36:50
问题 I'm trying to write TIFF IFDs, and I'm looking for a simple way to do the following (this code obviously is wrong but it gets the idea across of what I want): out.writeChar(12) (bytes 0-1) out.writeChar(259) (bytes 2-3) out.writeChar(3) (bytes 4-5) out.writeInt(1) (bytes 6-9) out.writeInt(1) (bytes 10-13) Would write: 0c00 0301 0300 0100 0000 0100 0000 I know how to get the writing method to take up the correct number of bytes (writeInt, writeChar, etc) but I don't know how to get it to write

Downloading file from DataURL in JavaScript

浪子不回头ぞ 提交于 2019-12-20 10:47:16
问题 From this string we get from DataURL, what's the best way to download this as a file? So far what I got was using a basic window.open("myDataURL"); , but I'm not able to change the file name in this way. window.open('data:application/msword;base64,0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAA PgADAP7/CQAGAAAAAAAAAAAAAAACAAAANQAAAAAAA AAAEAAANwAAAAIAAAD+////AAAAADQAAABsAA/', '_blank','height=300,width=400'); I was wondering if there's any way to handle this data properly. 回答1: you can add a download

Using PhoneGap FileWriter.write for “big” files

坚强是说给别人听的谎言 提交于 2019-12-20 10:33:32
问题 I have a problem in my PhoneGap app. I would like to write a file of 15 MB. If I try the OS pulls more and more memory and the app crashes without message. I can reproduce this on android and blackberry tablets. Is there a way to implement the writing more efficient? best regards fe.createWriter( (fw: any) => { fw.onwriteend = (e) => { fw.onwriteend = (e) => { callback(); } fw.write(data); } // write BOM (dead for now) fw.write(""); }, (error: any) => { alert("FileWriter Failed: " + error

how to edit my compare method

守給你的承諾、 提交于 2019-12-20 06:00:16
问题 I want to compare contens of my two txt files and write the different words in other file3.txt file I want to do compare method in this way to write another txt file. Also I dont have an error for coding I don't have a result. here is my code 回答1: I just ran your program with the following files and could not reproduce your problem. deneme1 abc def ghi deneme2 abc ghi klm And deneme3 was created with the following content: abc ghi EDIT It seems you want the opposite behaviour. Some of your

Writing output data to text file gives incomplete result in text file

早过忘川 提交于 2019-12-20 01:15:07
问题 I have 14 lists and each list have either numeric or string data. Size of each list is 32561. I have to output a file with the format: list1_element1 list2_element1.......................list14_element1 list1_element2 list2_element2.......................list14_element2 . . . . . list1_element32561 list2_element32561.......................list14_element32561 Each element in output file is separated by tab space. I did it like this in Java. out = new BufferedWriter(new FileWriter(fileName));

Java writing to a deleted file

纵然是瞬间 提交于 2019-12-18 19:42:10
问题 I'm using FileWriter to write to a file and noticed that even after I have deleted the file (out of process) the FileWriter does not throw any exception. Is this normal? 回答1: This depends on your operating system: On Windows, you'd typically not be able to delete an open file. On Unix, deleting an open file and continuing to write into it (or read from it) is perfectly acceptable. Once you delete the file, it will no longer have a directory entry. However, its contents will continue to exist

Writing data to text file in table format

随声附和 提交于 2019-12-17 13:45:12
问题 So far I have this: File dir = new File("C:\\Users\\User\\Desktop\\dir\\dir1\\dir2); dir.mkdirs(); File file = new File(dir, "filename.txt"); FileWriter archivo = new FileWriter(file); archivo.write(String.format("%20s %20s", "column 1", "column 2 \r\n")); archivo.write(String.format("%20s %20s", "data 1", "data 2")); archivo.flush(); archivo.close(); However. the file output looks like this: Which I do not like at all. How can I make a better table format for the output of a text file? Would

flush in java.io.FileWriter

女生的网名这么多〃 提交于 2019-12-17 09:39:40
问题 I have a question in my mind that, while writing into the file, before closing is done, should we include flush()??. If so what it will do exactly? dont streams auto flush?? EDIT: So flush what it actually do? 回答1: Writers and streams usually buffer some of your output data in memory and try to write it in bigger blocks at a time. flushing will cause an immediate write to disk from the buffer, so if the program crashes that data won't be lost. Of course there's no guarantee, as the disk may