I have a line in file config.properties  clean=true  and use following code to get this property  private static String clean; Properties prop = new Properties(); try {     prop.load(new FileInputStream("config.properties"));     clean = prop.getProperty("clean"); }   I use System.out.println("&gt;"+clean+"&lt;") to see the output and get "&gt;true&lt;", which indicates there is no blank, no \n  However, when I use  if (clean == "true") {               // program does not go here } else {     // program goes here }   what is the possible reason?...
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?
23361
I have the following code in Visual Studio 2005.      Dim OutFile As System.IO.StreamWriter     Try         OutFile = New System.IO.StreamWriter(Filename)        // Do stuff with OutFile     Catch Ex As Exception        // Handle Exception     Finally        If OutFile IsNot Nothing Then OutFile.Close()     End Try   But VS2005 brings up the warning for the line "If OutFile IsNot.." that      Variable 'OutFile' is used before it has been assigned a value. A null reference exception could result at runtime.   Is there some way of removing this warning by subtly altering the code or is there just a better way of doing what I'm trying to do?  Thanks  Rob
690
What is the difference between \par, \newline, \\ and blank lines? When should I use which one?
38862
In my application I load resources in this manner:  WinProcessor.class.getResource("repository").toString();   and this gives me:  file:/root/app/repository   (and I replace "file:" with empty string)   This works fine when I run my application from the IDE, but when I run the jar of my application:  java -jar app.jar   The path becomes:  jar:/root/app.jar!/repository   is there any way to solve this problem?  I'll use the "repository" dir name in order to create this:  ConfigurationContext ctx = (ConfigurationContext) ConfigurationContextFactory.createConfigurationContextFromFileSystem(repositoryString, null);   In the same manner, I'll get one file name (instead of a dir) and I'll use it this way:  System.setProperty("javax.net.ssl.trustStore", fileNameString)
114423
I am writing a drop-in replacement for a legacy application in Java.  One of the requirements is that the ini files that the older application used have to be read as-is into the new Java Application.  The format of this ini files is the common windows style, with header sections and key=value pairs, using # as the character for commenting.  I tried using the Properties class from Java, but of course that won't work if there is name clashes between different headers.  So the question is, what would be the easiest way to read in this INI file and access the keys?
37382
I'm a C++ guy learning Java. I'm reading Effective Java and something confused me. It says never to write code like this:  String s = new String("silly");   Because it creates unnecessary String objects. But instead it should be written like this:  String s = "No longer silly";   Ok fine so far...However, given this class:  public final class CaseInsensitiveString {     private String s;     public CaseInsensitiveString(String s) {         if (s == null) {             throw new NullPointerException();         }         this.s = s;     }     :     : }  CaseInsensitiveString cis = new CaseInsensitiveString("Polish"); String s = "polish";    Why is the first statement ok? Shouldn't it be   CaseInsensitiveString cis = "Polish"; How do I make CaseInsensitiveString behave like String so the above statement is OK (with and without extending String)? What is it about String that makes it OK to just be able to pass it a literal like that? From my understanding there is no "copy constructor" concept in Java?
42437
I've been trying to optimize my code to make it a little more concise and readable and was hoping I wasn't causing poorer performance from doing it.  I think my changes might have slowed down my application, but it might just be in my head.  Is there any performance difference between:  Command.Parameters["@EMAIL"].Value = email ?? String.Empty;   and  Command.Parameters["@EMAIL"].Value = (email == null) ? String.Empty: email;   and  if (email == null) {     Command.Parameters["@EMAIL"].Value = String.Empty } else {     Command.Parameters["@EMAIL"].Value = email }   My preference for readability would be the null coalescing operator, I just didn't want it to affect performance.
118840
I hope this question is in order here although it rather asks for a "best practice" than for some "solution".  I am wondering to what extent blank/empty lines in the code (I am using pdflatex) should be avoided and e.g. be replaced by %. My source code is full of single blank lines e.g. before the start of a new section, between math/text environments in order to increase the readability of the code.  Are such blank lines going to be ignored anyway (I guess this might depend on the specific instance?)/only cause minor vertical space in the PDF, or are they deemed "bad practice" and should be avoided?  All I could find was  according to which - I take it - blank lines should mostly be avoided apart from before tabular and array environments.
26226
I need a robust and simple way to remove illegal path and file characters from a simple string. I've used the below code but it doesn't seem to do anything, what am I missing?  using System; using System.IO;  namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             string illegal = "\"M&lt;&gt;\"\\a/ry/ h**ad:&gt;&gt; a\\/:*?\"&lt;&gt;| li*tt|le|| la\"mb.?";              illegal = illegal.Trim(Path.GetInvalidFileNameChars());             illegal = illegal.Trim(Path.GetInvalidPathChars());              Console.WriteLine(illegal);             Console.ReadLine();         }     } }
2343
I've been using the idiom below for some time now. And it seems to be the most wide-spread, at least on the sites I've visited.  Is there a better/different way to read a file into a string in Java?  private String readFile(String file) throws IOException {     BufferedReader reader = new BufferedReader(new FileReader (file));     String         line = null;     StringBuilder  stringBuilder = new StringBuilder();     String         ls = System.getProperty("line.separator");      try {         while((line = reader.readLine()) != null) {             stringBuilder.append(line);             stringBuilder.append(ls);         }          return stringBuilder.toString();     } finally {         reader.close();     } }
104904
I really should be able to get this, but I'm just to the point where I think it'd be easier to ask.  In the C# function:  public static T GetValue&lt;T&gt;(String value) where T:new() {    //Magic happens here }   What's a good implementation for the magic? The idea behind this is that I have xml to parse and the desired values are often primitives (bool, int, string, etc.) and this is the perfect place to use generics... but a simple solution is eluding me at the moment.  btw, here's a sample of the xml I'd need to parse  &lt;Items&gt;     &lt;item&gt;         &lt;ItemType&gt;PIANO&lt;/ItemType&gt;         &lt;Name&gt;A Yamaha piano&lt;/Name&gt;         &lt;properties&gt;             &lt;allowUpdates&gt;false&lt;/allowUpdates&gt;             &lt;allowCopy&gt;true&lt;/allowCopy&gt;         &lt;/properties&gt;        &lt;/item&gt;     &lt;item&gt;         &lt;ItemType&gt;PIANO_BENCH&lt;/ItemType&gt;         &lt;Name&gt;A black piano bench&lt;/Name&gt;         &lt;properties&gt;             &lt;allowUpdates&gt;true&lt;/allowUpdates&gt;             &lt;allowCopy&gt;false&lt;/allowCopy&gt;             &lt;url&gt;www.yamaha.com&lt;/url&gt;         &lt;/properties&gt;     &lt;/item&gt;     &lt;item&gt;         &lt;ItemType&gt;DESK_LAMP&lt;/ItemType&gt;         &lt;Name&gt;A Verilux desk lamp&lt;/Name&gt;         &lt;properties&gt;             &lt;allowUpdates&gt;true&lt;/allowUpdates&gt;             &lt;allowCopy&gt;true&lt;/allowCopy&gt;             &lt;quantity&gt;2&lt;/quantity&gt;         &lt;/properties&gt;     &lt;/item&gt; &lt;/Items&gt;
81623
I am writing a script, I want to check if the first line of the file matches a certain pattern and if it does then print out the file. How can I achieve this?  How do I check for pattern? Is there a way to check for pattern and based on the output do something..  EDIT: Please take a look at this question:   I want something like this, but none of them worked for me. I basically want to check if the first line matches a regex pattern or not and based on that print the lines of file.
118610
I remember seeing a while ago that there is some method in maybe the Reflection namespace that would recursively run ToString() on all of an object's properties and format it nicely for display.    Yes, I know everything I could want will be accessible through the debugger, but I'm wondering if anyone knows that command?
83401
I have a question about the meaning (evaluation) of Boolean variables in return statements in Java.  I know that:  if (var) { ... }   is the same as:  if (var==true) { ... }   In the second case we explicitly say var==true, but we don't need to do this, because Java evaluates var as true anyway. I hope I have understood this right.  My question is: is it the same when Boolean variables are returned? When we have a return statement?  For example, a task specifies: the method looksBetter() will return true only if b &lt; a. My solution was:  public boolean looksBetter() {      if (b &lt; a) {          return true;      } else {          return false;      } }   The simple answer was:  public boolean lookBetter() {       return b &lt; a; }   So, it seems that here we have again this implicit assumption that in case b &lt; a == true, the return of the method is true. I am sorry ... it seems very trivial, but I am somehow not comfortable with this, and I don't know why. Thank you.
29862
So what I have right now is something like this:  PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public);   where obj is some object.  The problem is some of the properties I want aren't in obj.GetType() they're in one of the base classes further up.  If I stop the debugger and look at obj, the I have to dig through a few "base" entries to see the properties I want to get at.  Is there some binding flag I can set to have it return those or do I have to recursively dig through the Type.BaseType hierarchy and do GetProperties on all of them?
17511
So this seems pretty basic but I can't get it to work.  I have an Object, and I am using reflection to get to it's public properties.  One of these properties is static and I'm having no luck getting to it.  Public Function GetProp(ByRef obj As Object, ByVal propName as String) as PropertyInfo     Return obj.GetType.GetProperty(propName)  End Function   The above code works fine for Public Instance properties, which up until now is all that I have needed.  Supposedly I can use BindingFlags to request other types of properties (private, static), but I can't seem to find the right combination.  Public Function GetProp(ByRef obj As Object, ByVal propName as String) as PropertyInfo     Return obj.GetType.GetProperty(propName, Reflection.BindingFlags.Static Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public)  End Function   But still, requesting any Static members return nothing.  .NET reflector can see the static properties just fine, so clearly I am missing something here.
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.