I got a file contains two lines:  aaaaa bbbbb   I used these lines to read each lines:      File f=new File("D:\\xxx.dat");     FileReader fr = new FileReader(f);     BufferedReader br = new BufferedReader(fr);     String str;     String tmpp="";     while ((str = br.readLine()) != null) {          if(str=="bbbbb")          {          System.out.print(str);          }     }   but I got nothing as a result.why?
93
I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.  Is == bad? When should it and should it not be used? What's the difference?
15664
I have a text file named links.txt which looks like this  link1 link2 link3   I want to loop through this file line by line and perform an operation on every line. I know I can do this using while loop but since I am learning, I thought to use a for loop. I actually used command substitution like this   a=$(cat links.txt)   Then used the loop like this  for i in $a; do ###something###;done   Also I can do something like this  for i in $(cat links.txt); do ###something###; done   Now my question is when I substituted the cat command output in a variable a, the new line characters between link1 link2 and link3 are removed and is replaced by spaces  echo $a   outputs  link1 link2 link3  and then I used the for loop. Is it always that a new line is replaced by space when we do a command substitution??  Regards
3477
I don't quite understand how the computer reads this command.  cat file1 file2 1&gt; file.txt 2&gt;&amp;1  If I understand, 2&gt;&amp;1 simply redirect Standard Error to Standard Output.  By that logic, the command reads to me as follows:    concatenate files file1 and file2.  send stdout from this operation to file.txt. send stderr to stdout. end?   I'm not sure what the computer's doing. By my logic, the command should be   cat file1 file2 2&gt;&amp;1 &gt; file.txt   but this is not correct.
51462
If I grep a document that contains the following:  ThisExampleString  ...for the expression This*String or *String, nothing is returned. However, This* returns the above line as expected.  Whether the expression is enclosed in quotes makes no difference.  I thought the asterisk indicated any number of unknown characters? Why does it only work if it's at the start of the expression? If this is intended behavior, what do I use instead of the expressions This*String and *String?
14429
What's the difference between double quotes and single quote in C#?  I coded a program to count how many words are in a file  using System; using System.IO; namespace Consoleapp05 {     class Program     {         public static void Main(string[] args)         {             StreamReader sr = new StreamReader(@"C:\words.txt");             string text = sr.ReadToEnd();             int howmany = 0;             int howmany2 = 0;              for(int i = 0; i &lt; text.Length; i++)             {                 if(text[i] == " ")                 {                 howmany++;                 }             }             howmany2 = howmany + 1;             Console.WriteLine("It is {0} words in the file", howmany2);             Console.ReadKey(true);         }     } }   This gives me an error because of the double quotes. My teacher told me to use single quote instead, but he didn't tell me why. So what's the difference between double quotes and single quote in C#?
41776
I'm sure there are many ways to do this: how can I count the number of lines in a text file?  $ &lt;cmd&gt; file.txt 1020 lines
84318
Let's say I have a text file like:  john george james stewert   with every name on a separate line.  I want to read the lines of this text file and create a text file for each name, like: john.txt, george.txt etc.  How can I do this in Bash?
76849
I am trying to write a function to determine if a file exists. The two methods prove to return inconsistent results (fileExists() seems to provide accurate results, compared to isFileFound(), that returns false positives - i would have expected an exception when trying to create the instance).   protected bool isFileFound(string path, string fileName)     {         System.IO.FileInfo fi = null;          bool found = false;         try         {             fi = new System.IO.FileInfo(path + fileName);             found = true;         }         catch (Exception e)         {             baselogger.Fatal(e.Message + " " + e.StackTrace + " \n" + path + fileName);         }          return found;     }      protected bool fileExists(string path, string pattern)     {         bool success = false;          try         {             success = File.Exists(path + pattern);         }         catch (Exception e)         {             baselogger.Warn(e.Message + " " + e.StackTrace + " " + e.Source);         }          return success;     }   Neither seems to be able to resolve a UNC path of the following syntax: \\abcserver\c$\xyzfolder\foo.bar  Any idea why the unc path is failing for these methods would be greatly appreciated.
66091
I'm trying to use grep to show only lines containing either of the two words, if only one of them appears in the line, but not if they are in the same line.  So far I've tried  grep pattern1 | grep pattern2 | ... but didn't get the result I expected.
122315
I am using grep to recursively look through files and pull out all strings matching a pattern. I want to print the file path: matched string.   -H should print the file path for each entry  -o should print just the matched string, omitting everything else on the line I don't want  -r recursive search.   I run:  grep -Hro [pattern] .   Most of the matches print with the filename, however a few just print the matched string. Are -H and -o mutually exclusive in that -o is trimming the filename?  Sample Output:   ./path/foo.txt: foo ./path/bar.txt: foo ./path/baz.txt: foo foo ./path/baz.txt: foo   The 4th foo match should also have a file associated with it, but it is not being printed.   I am getting behavior similar to this , however I am using the -H flag, and the "/dev/null" solution does not work either.  $ grep --version grep (BSD grep) 2.5.1-FreeBSD   EDIT:  After further exploring each match where the regex was not getting the file name, they all occur when two strings are matched on the same line.  For example this text file  foo foo sometext foo foo   Outputs  ./path/foo.txt: foo ./path/foo.txt: foo foo ./path/foo.txt: foo   Anyone know a solution to get both matches to print a filename?
26310
I have a file servers.txt, with list of servers:  server1.mydomain.com server2.mydomain.com server3.mydomain.com   when I read the file line by line with while and echo each line, all works as expected. All lines are printed.  $ while read HOST ; do echo $HOST ; done &lt; servers.txt server1.mydomain.com server2.mydomain.com server3.mydomain.com   However, when I want to ssh to all servers and execute a command, suddenly my while loop stops working:  $ while read HOST ; do ssh $HOST "uname -a" ; done &lt; servers.txt Linux server1 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux   This only connects to the first server in the list, not to all of them. I don't understand what is happening here. Can somebody please explain?  This is even stranger, since using for loop works fine:  $ for HOST in $(cat servers.txt ) ; do ssh $HOST "uname -a" ; done Linux server1 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux Linux server2 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux Linux server3 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux   It must be something specific to ssh, because other commands work fine, such as ping:  $ while read HOST ; do ping -c 1 $HOST ; done &lt; servers.txt
15568
I'm wondering what techniques and/or library to use to implement the functionality of the linux command "tail -f ".  I'm essentially looking for a drop in add-on/replacement for java.io.FileReader.  Client code could look something like this:  TailFileReader lft = new TailFileReader("application.log"); BufferedReader br = new BufferedReader(lft); String line; try {   while (true) {     line= br.readLine();     // do something interesting with line   } } catch (IOException e) {   // barf }   The missing piece is a reasonable implementation of TailFileReader.  It should be able to read parts of the file that exist before the file is opened as well as the lines that are added.
94748
How you can read a file (text or binary) from a batch file? There is a way to read it in a binary mode or text mode?
24166
It took me a while to word this best.  Problem 1:  Is there a way to find lines in a text file that have the same string at the beginning and the same strings at the end?   Beginning and end strings not necessarily equal to each other.  Text between the two strings is not consistant in number of characters or number of hyphens.  A hyphen is in between notable strings (which I added to my large text file).  For example:  File:    AAAAA-tjhut-ghyut-FILE1.txt   BBB-fhuj-dnbhg-frt76-FILE2.txt   AAAAA-ghtujukr-hgute7gr-xhyutt-FILE1.txt   AAAAA-VGYT-4d7tt-xhyutt-FILE1.txt   CCCC-fjuk-dryy-fhuj-FILE3.txt   Lines to detect:    AAAAA-tjhut-ghyut-FILE1.txt   AAAAA-ghtujukr-hgute7gr-xhyutt-FILE1.txt   AAAAA-VGYT-4d7tt-xhyutt-FILE1.txt   Problem 2:  Is there a way to remove all but one of the lines containing the duplicate strings at the beginning and end from above?  Before:    AAAAA-tjhut-ghyut-FILE1.txt   AAAAA-ghtujukr-hgute7gr-xhyutt-FILE1.txt   AAAAA-VGYT-4d7tt-xhyutt-FILE1.txt   After:    AAAAA-tjhut-ghyut-FILE1.txt   Big thanks for any advice.  Note: I think using the occurances of the first hyphen and last hyphen can be used to locate the duplicate strings.
11099
I have the following file:  abcde kwakwa &lt;0x1A&gt; line3 linllll   Where &lt;0x1A&gt; represents a byte with the hex value of 0x1A. When attempting to read this file in Python as:  for line in open('t.txt'):     print line,   It only reads the first two lines, and exits the loop.  The solution seems to be to open the file in binary (or universal newline mode) - 'rb' or 'rU'. Can you explain this behavior ?
71595
It has always bothered me that the only way to copy a file in Java involves opening streams, declaring a buffer, reading in one file, looping through it, and writing it out to the other steam.  The web is littered with similar, yet still slightly different implementations of this type of solution.  Is there a better way that stays within the bounds of the Java language (meaning does not involve exec-ing OS specific commands)?  Perhaps in some reliable open source utility package, that would at least obscure this underlying implementation and provide a one line solution?
85028
Comparing     grep '.*[s]' file   with     grep .*[s] file   Why do you need quotation marks to let this work properly? In the second case, grep seems trying to inspect every file with a period.