in the below code i can able to convert a file to gzip but here i am providing the location for input &amp; output staticly. But i need to provide the file name dynamically   example here i am using   String source_filepath = "C:\\Users\\abc\\Desktop\\home6.jpg"; String destinaton_zip_filepath =C:\\Users\\abc\\Desktop\\home6.gzip";   here in place of home6.jpg i can give anything dynamically   import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream;   public class CompressFileGzip {      public static void main(String[] args) {          String source_filepath = "C:\\Users\\abc\\Desktop\\home6.jpg";          String destinaton_zip_filepath = "C:\\Users\\abc\\Desktop\\home6.gzip";            CompressFileGzip gZipFile = new CompressFileGzip();          gZipFile.gzipFile(source_filepath, destinaton_zip_filepath);      }      public void gzipFile(String source_filepath, String destinaton_zip_filepath) {          byte[] buffer = new byte[1024];         try {                 FileOutputStream fileOutputStream =new FileOutputStream(destinaton_zip_filepath);              GZIPOutputStream gzipOuputStream = new GZIPOutputStream(fileOutputStream);              FileInputStream fileInput = new FileInputStream(source_filepath);              int bytes_read;              while ((bytes_read = fileInput.read(buffer)) &gt; 0) {                  gzipOuputStream.write(buffer, 0, bytes_read);              }              fileInput.close();              gzipOuputStream.finish();              gzipOuputStream.close();              System.out.println("The file was compressed successfully!");          } catch (IOException ex) {              ex.printStackTrace();          }      }  }
20243
What is a good way of parsing command line arguments in Java?
77902
I have a small text file that I'd like to read into a scalar variable exactly as it is in the file (preserving line separators and other whitespace).  The equivalent in Python would be something like  buffer = ""  try:     file = open("fileName", 'rU')     try:         buffer += file.read()     finally:         file.close() except IOError:     buffer += "The file could not be opened."   This is for simply redisplaying the contents of the file on a web page, which is why my error message is going into my file buffer.
7589
this is my first post so I hope you'll be kind enough to forgive me if I make some formatting mistakes or else. This is one question I've had for quite some time and while looking for answers I have more questions arising. I was trying to find a way to compress pictures in a folder using convert in one command.  First I simply tried:  convert * -resize 50% Pic.jpg   However this returned the compressed pictures named Pic-0.jpg, Pic-1.jpg, etc. (I also tried $(find ".JPG") instead of * but this is expanded to the same. In order to have the formatting I wanted I tried:  convert * -resize 50% Pic_lorez_{1..3}.jpg   But this seemed to take the Pic_lorez_2.jpg and Pic3_lorez_3.jpg as input files, not output files. So following the advice of a friend, I tried:  find . -name "*.JPG" -exec convert {} -resize 50% {} \;   This compress all files under the same name but doesn't allow to put the name I want to these files (for example, Pic_lorez_{1,2,3,4,5,..100}.jpg).  Would xargs make it possible? Something along the lines of:  find . -name "*.JPG" | xargs convert ???   I couldn't figure out the proper syntax with xargs. I finally tried a direct pipe of find into convert but convert didn't seem to be able to work that way.
5289
I have a rather big number of source files that I need parse and extract all string literals and put them in a file as play old java constant. For exemple:  Label l = new Label("Cat");   Would become:  Label l = new Label(Constants.CAT);   And in Constants.java I would have:  public final static String CAT = "Cat";   I do not want the strings to be externalized in a property text file. One reason is for consistency and code readability. The other is that our client code uses GWT, which does not support Java property text file mechanism.  I could write some sort of parser (using ant replace task maybe)? But I wondered if an IDE already does this sort of thing automatically.
60818
I need to test if a file is a shortcut. I'm still trying to figure out how stuff will be set up, but I might only have it's path, I might only have the actual contents of the file (as a byte[]) or I might have both.  A few complications include that I it could be in a zip file (in this cases the path will be an internal path)
100638
As a simple example, I have a bunch of source code files. I want to store the "head" command output to a variable for all these files.  I tried:  output=$(head $file)   but what happened is that this automatically trimmed all \n characters when storing the output to a variable.  How do I store the command output as is without removing \n characters?
50127
What would be the most efficient method of reading a text file into a dynamic one-dimensional array? reallocing after every read char seems silly, reallocing after every read line doesn't seem much better. I would like to read the entire file into the array. How would you do it?
19556
Yes,  but there must be a canonical or most efficient or most concise way. I'll add answers I know of and see what percolates to the top.  To be clear, the question is how best to read the contents of a file into a string. One solution per answer.
58268
In Java, given a java.net.URL or a String in the form of http://www.example.com/some/path/to/a/file.xml , what is the easiest way to get the file name, minus the extension? So, in this example, I'm looking for something that returns "file".   I can think of several ways to do this, but I'm looking for something that's easy to read and short.
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.
59328
I'd like to find all the Java files containing keyword File.createTempFile with the line number. Here's what I did:  $ find . -name "*.java" | xargs grep -n "File.createTempFile" ./nuxeo-studio-test/src/test/java/com/nuxeo/studio/web/StudioServletTest.java:162:            File tmpFile = File.createTempFile(jarEntryName, null, Environment.getDefault().getTemp()); ./nuxeo-studio-ui/src/main/java/com/nuxeo/studio/core/builders/WorkflowFeatureBuilder.java:421:            tmp = File.createTempFile(".workflow-to-zip", ".tmp", workspace.getRoot()); ./nuxeo-studio-ui/src/test/java/com/nuxeo/studio/core/backup/TestS3Backup.java:101:        File tempFile = File.createTempFile(RandomStringUtils.randomAlphabetic(5), ".tmp"); ./nuxeo-studio-ui/target/classes/com/nuxeo/studio/core/builders/WorkflowFeatureBuilder.java:421:            tmp = File.createTempFile(".workflow-to-zip", ".tmp", workspace.getRoot()); ./nuxeo-studio-web/src/main/java/com/nuxeo/studio/web/v1/ProjectResource.java:191:            File zip = File.createTempFile(filename + "-", ".zip", tempRepo);   However, I found this result too long to read. I'd like to keep only the file path and the line number. Here's expected result:  ./nuxeo-studio-test/src/test/java/com/nuxeo/studio/web/StudioServletTest.java:162 ./nuxeo-studio-ui/src/main/java/com/nuxeo/studio/core/builders/WorkflowFeatureBuilder.java:421 ./nuxeo-studio-ui/src/test/java/com/nuxeo/studio/core/backup/TestS3Backup.java:101 ./nuxeo-studio-ui/target/classes/com/nuxeo/studio/core/builders/WorkflowFeatureBuilder.java:421 ./nuxeo-studio-web/src/main/java/com/nuxeo/studio/web/v1/ProjectResource.java:191   Could somebody teach me how to do so?
6637
Is there a good clean conventional way to do this?  For example, if there are any ".gz" files in a certain directory, I want to unzip them.  But if there aren't, I don't want to see any error.  If I use gzip -d /mydir/*.gz, I get the error:  gzip: /mydir/*.gz: No such file or directory   If I first shopt -s nullglob and then gzip -d /mydir/*.gz, I get the following:  gzip: compressed data not read from a terminal. Use -f to force decompression. For help, type: gzip -h   I have one method I know will work, which I'm posting as an answer.  I'm wondering if there's a better/cleaner way.  POSIX compatibility is a bonus, but not required.
59969
I can easily find files with given string in name:  me@comp:/usr/local/hydra/hydra-7.4.2$ find -D stat -name "*hack*" ./hack881663129.txt ./hack881663129_7.txt ./hack881663129_5.txt ./hack881663129_4.txt ./hack881663129_4_7.txt ./hack881663129_6.txt ./hack881663129_1_6.txt ./hack881663129_8.txt   How can I also print file details, like size, date of creation etc.?
5571
When handling log files, some end up as gzipped files thanks to logrotate and others not. So when you try something like this:  $ zcat *   you end up with a command line like zcat xyz.log xyz.log.1 xyz.log.2.gz xyz.log.3.gz and then with:  gzip: xyz.log: not in gzip format   Is there a tool that will take the magic bytes, similar to how file works, and use zcat or cat depending on the outcome so that I can pipe the output to grep for example?  NB: I know I can script it, but I am asking whether there is a tool out there already.
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}
108724
I am writing a bash function that takes an input as a file name and make sure it starts with an alpha character however the grep I am using to test the filename is accepting any input it is given  here is my code...  function getname(){ fname=$1; if [ ! $1 ]; then         read -p "Enter a file name: " fname; fi; until ( grep -E '^[a-zA-Z_]\w+' &lt;&lt;&lt; "$fname" &gt; /dev/null 2&gt;1&amp;); do     if [ -d $fname ]; then             echo "Paths are not a legal file name.";         fi;         read -p "Enter a legal file name: " fname; done }   it will properly accept either 1 or no parameters and prompt for a file name(if no parameter) but it will accept any string that is passed to it. so how would I properly grep the string stored as fname?
15324
I wish to store a few folders in a zip file and then convert that zip file into a jar.  I have the following folders : META-INF and org.  I tried the following command:  zip META-INF/ org/ wordcount.zip   But wordcount.zip was not created.