I want to overwrite to a text file using java by first create a new file and then delete the old one and finally rename the file I just created with the same name of my old file. But my code does not work.                  File oldFile = new File("charList.txt");                     File newFile = new File("new.txt");                     oldFile.deleteOnExit();                      if(oldFile.delete()){                        newFile.renameTo(oldFile);                     }
121298
I have a file I need to rename to that of an existing file. This is a copy, modify, replace original operation on an existing JAR file. I've got the first two steps done, I just need help with the replace original bit. What's the best way to rename the new version of the JAR to that of the old. The old JAR doesn't need preserving and I don't want to have a copy of the new with its initial name sticking around.   I have commons lang and io already, so if there's a method I've missed, that would be great.
106661
I have an input file, say, file1, which appears as follows:  WRITE=2 START=1 SMEAR=0 MIN=6 MAX=100   My problem is to search for the string SMEAR and if the string exists replace the entire line with SMEAR=-5. However, if the SMEAR does not exist, then I need to insert a new line SMEAR=-5. Ultimately the line SMEAR=-5 should appear only once. I can replace the string using sed:   sed -i '/SMEAR/c\SMEAR=-5' file1   Or I can insert the string as a new line    sed -i '10i SMEAR=-5' file1   But how can I combine them based on the existence of the string in the file?  EDIT: Also it would be helpful if I can pass the value of SMEAR as a variable.
89325
How do I clear the contents of a file before writing to it? For example:  echo one &gt; filename.tmp # filename.tmp now contains "one"  echo two &gt; filename.tmp # filename.tmp should now contain "two", not "one" and "two"   My goal is:   Start a listener  $ nc -l 7007 &gt; /var/tmp/test.log  Send some data  $ telnet localhost 7007 hi second_word  Test the file  $ cat /var/tmp/test.log second_word    I don't want "hi" to show up in the log; I want "second_word" to have replaced it
103401
I am trying to replace strings in a file A:  Hello Peter, how is your dad? where is mom?    where the strings to be replaced are in file B:  Peter dad mom   and their corresponding replacements are in file C:  John wife grandpa   Expected outcome:  Hello John, how is your wife? where is grandpa?   Can I edit file A, replacing the value in file B by using the value from the corresponding line in file C?  What I have done so far:   cat 1.txt | sed -e "s/$(sed 's:/:\\/:g' 2.txt)/$(sed 's:/:\\/:g' 3.txt)/" &gt; 4.txt   it works if there is only one line in file B &amp; file C, if there is more than one line, it won't work.
92456
Alright, I have been doing the following (variable names have been changed):   FileInputStream fis = null; try {     fis = new FileInputStream(file);      ... process ...  } catch (IOException e) {     ... handle error ... } finally {     if (fis != null)         fis.close(); }    Recently, I started using FindBugs, which suggests that I am not properly closing streams.  I decide to see if there's anything that can be done with a finally{} block, and then I see, oh yeah, close() can throw IOException.  What are people supposed to do here?  The Java libraries throw too many checked exceptions.
27778
Is there an efficient way to create a file with a given size in Java?  In C it can be done with  (see ).   Most people would just write n dummy bytes into the file, but there must be a faster way. I'm thinking of  and also of …
1394
Replacing strings in files based on certain search criteria is a very common task. How can I    replace string foo with bar in all files in the current directory? do the same recursively for sub directories? replace only if the file name matches another string? replace only if the string is found in a certain context? replace if the string is on a certain line number? replace multiple strings with the same replacement replace multiple strings with different replacements
48194
With the commands \openout and \write I output text into a file. I want this file to have a name of containing an underscore (eg. name_01.txt), but I'm unable to get the underscore in the filename. My attempt (with \_):   \documentclass{article} \begin{document} \newwrite\tempfile \immediate\openout\tempfile="name\_01.txt" \immediate\write\tempfile{this is interesting} \immediate\closeout\tempfile \end{document}
91691
I have two files like:  file 1 :  a stringori to replace, this stringori comme again  other stringori2 to replace, stringori2 commes again ...   file 2:  stringori newstring stringori2  newstring2 ...   I want to get the result:  a newstring to replace, this newstring comme again  other newstring2 to replace, newstring2 commes again
112078
I am trying to determine a perl method to add an extension to a list of files. I can already rename a list of files with a certain extension to some other extension, but I need to be able to do this for any file name, whether or not it already has an extension.  For example, I have a list of files such as:  file1 file2 file3 ps.dir   And I want to rename them ALL to filename.extension:  file1.bad file2.bad file3.bad ps.dir.bad
4546
From:  How do I modify this to only delete the first version of the file it sees.  Open Terminal from Spotlight or the Utilities folder Change to the directory (folder) you want to search from (including sub-folders) using the cd command. At the command prompt type cd  for example cd ~/Documents to change directory to your home Documents folder At the command prompt, type the following command:   find . -size 20 \! -type d -exec cksum {} \; | sort | tee /tmp/f.tmp | cut -f 1,2 -d ' ' | uniq -d | grep -hif – /tmp/f.tmp &gt; duplicates.txt   This method uses a simple checksum to determine whether files are identical. The names of duplicate items will be listed in a file named duplicates.txt in the current directory. Open this to view the names of identical files There are now various ways to delete the duplicates. To delete all the files in the text file, at the command prompt type:   while read file; do rm "$file"; done &lt; duplicates.txt
55252
I have a text file and want to keep lines started with &lt;Path&gt;, and delete all the other lines.  How can I do it?
48423
I want to create multiple files at a time using names from a file list.txt, how can I do it?  Sample list.txt:  EOG090W002U EOG090W00C1 EOG090W00DC EOG090W00DE EOG090W00E5 EOG090W00HR EOG090W00MH EOG090W00MS EOG090W00PB EOG090W00U4 EOG090W00UK EOG090W00WM EOG090W00WR   Suppose I have this list.txt containing some id-numbers. Now I want to make separate files using these ids as the name (e.g. EOG090W002U_M0.ctl, EOG090W00C1_M0.ctl, EOG090W00DC_M0.ctl). Also, the contents of the files need to be change accordingly. For example the content of EOG090W002U_M0.ctl, EOG090W00C1_M0.ctl file will be   seqfile = EOG090W002U_p.phy treefile = Constant.txt outfile = EOG090W002U_M0_mlc   or  seqfile = EOG090W00C1_p.phy treefile = Constant.txt outfile = EOG090W00C1_M0_mlc   where *.phy and Constant.txt will be provided in the same folder.
92432
I want to both read from and write to a file. This doesn't work.   static void Main(string[] args) {     StreamReader sr = new StreamReader(@"C:\words.txt");     StreamWriter sw = new StreamWriter(@"C:\words.txt"); }   How can I both read from and write to a file in C#?
98998
I need to do create a file with a filename such as :&gt;?, is this possible somehow? Windows stops it.
50533
I need to perform Diffs between Java strings. I would like to be able to rebuild a string using the original string and diff versions. Has anyone done this in Java? What library do you use?  String a1; // This can be a long text String a2; // ej. above text with spelling corrections String a3; // ej. above text with spelling corrections and an additional sentence  Diff diff = new Diff(); String differences_a1_a2 = Diff.getDifferences(a,changed_a); String differences_a2_a3 = Diff.getDifferences(a,changed_a);     String[] diffs = new String[]{a,differences_a1_a2,differences_a2_a3}; String new_a3 = Diff.build(diffs); a3.equals(new_a3); // this is true
69617
I have created two files:  sample.txt and sample.txt  (the second file contains some hidden characters, e.g. a space). How can I remove the latest modified file? I'm using Linux.