How do I save a String to a text file using Java?

2021-6-3 anglehua

In Java, I have text from a text field in a String variable called "text". How can I save the contents of the "text" variable to a file? If you're simply outputting text, rather than any binary data, the following will work: PrintWriter out = new PrintWriter("filename.txt"); Then, write your Strin...

阅读全文>>

评论(0) 浏览(97)

decompiling DEX into Java sourcecode

2021-6-3 anglehua

How can one decompile Android DEX (VM bytecode) files into corresponding Java sourcecode? It's easy Get these tools: dex2jar to translate dex files to jar files jd-gui to view the java files in the jar The source code is quite readable as dex2jar makes some optimizations. Procedure: And here's ...

阅读全文>>

评论(0) 浏览(104)

Removing whitespace from strings in Java

2021-6-3 anglehua

I have a string like this: mysz = "name=john age=13 year=2001"; I want to remove the whitespaces in the string. I tried trim() but this removes only whitespaces before and after the whole string. I also tried replaceAll("\\W", "") but then the = also gets removed. How can I achieve a string with: ...

阅读全文>>

评论(0) 浏览(84)

What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?

2021-6-3 anglehua

I am going through some blogs on SpringSource and in one of the blogs, author is using @Inject and I suppose he can also use @Autowired. Here is the piece of code: @Inject private CustomerOrderService customerOrderService; I am not sure about the difference between @Inject and @Autowired and would a...

阅读全文>>

评论(0) 浏览(130)

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”

2021-6-3 anglehua

My application is to be deployed on both tcServer and WebSphere 6.1. This application uses ehCache and so requires slf4j as a dependency. As a result I've added the slf4j-api.jar (1.6) jar to my war file bundle. The application works fine in tcServer except for the following error: SLF4J: Failed to ...

阅读全文>>

评论(0) 浏览(171)

How to read all files in a folder from Java?

2021-6-3 anglehua

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. How to read all the files in a folder through Java?...

阅读全文>>

评论(0) 浏览(102)

Understanding checked vs unchecked exceptions in Java

2021-6-3 anglehua

Joshua Bloch in "Effective Java" said that Use checked exceptions for recoverable conditions and runtime exceptions for programming errors (Item 58 in 2nd edition) Let's see if I understand this correctly. Here is my understanding of a checked exception: try{ String userInput = //read i...

阅读全文>>

评论(0) 浏览(112)

How do I compare strings in Java?

2021-6-3 anglehua

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. I've been using the == operator in my program to compare all my strings so far. However, I ran i...

阅读全文>>

评论(0) 浏览(113)

Converting ISO 8601-compliant String to java.util.Date

2021-6-3 anglehua

I am trying to convert an ISO 8601 formatted String to a java.util.Date. I found the pattern yyyy-MM-dd'T'HH:mm:ssZ to be ISO8601-compliant if used with a Locale (compare sample). However, using the java.text.SimpleDateFormat, I cannot convert the correctly formatted String 2010-01-01T12:00:00+01:0...

阅读全文>>

评论(0) 浏览(112)

How to convert a Map to List in Java?

2021-6-3 anglehua

What is the best way to convert a Map<key,value> to a List<value>? Just iterate over all values and insert them in a list or am I overlooking something? List<Value> list = new ArrayList<Value>(map.values()); assuming: Map<Key,Value> map; The issue here is that Map h...

阅读全文>>

评论(0) 浏览(113)

What is a raw type and why shouldn't we use it?

2021-6-3 anglehua

Questions: What are raw types in Java, and why do I often hear that they shouldn't be used in new code? What is the alternative if we can't use raw types, and how is it better? What is a raw type? The Java Language Specification defines a raw type as follows: JLS 4.8 Raw Types A raw type is defi...

阅读全文>>

评论(0) 浏览(333)

How to append text to an existing file in Java?

2021-6-3 anglehua

I need to append text repeatedly to an existing file in Java. How do I do that? Are you doing this for logging purposes? If so there are several libraries for this. Two of the most popular are Log4j and Logback. Java 7+ For a one-time task, the Files class makes this easy: try { Files.write(Pa...

阅读全文>>

评论(0) 浏览(124)

What is the volatile keyword useful for?

2021-6-3 anglehua

At work today, I came across the volatile keyword in Java. Not being very familiar with it, I found this explanation. Given the detail in which that article explains the keyword in question, do you ever use it or could you ever see a case in which you could use this keyword in the correct manner? v...

阅读全文>>

评论(0) 浏览(121)

How to verify that a specific method was not called using Mockito?

2021-6-3 anglehua

How to verify that a method is not called on an object's dependency? For example: public interface Dependency { void someMethod(); } public class Foo { public bar(final Dependency d) { ... } } With the Foo test: public class FooTest { @Test public void dependencyIsNotCa...

阅读全文>>

评论(0) 浏览(103)

How do I invoke a Java method when given the method name as a string?

2021-6-3 anglehua

If I have two variables: Object obj; String methodName = "getName"; Without knowing the class of obj, how can I call the method identified by methodName on it? The method being called has no parameters, and a String return value. It's a getter for a Java bean. Coding from the hip, it would be some...

阅读全文>>

评论(0) 浏览(90)

Can I add jars to Maven 2 build classpath without installing them?

2021-6-3 anglehua

Maven 2 is driving me crazy during the experimentation / quick and dirty mock-up phase of development. I have a pom.xml file that defines the dependencies for the web-app framework I want to use, and I can quickly generate starter projects from that file. However, sometimes I want to link to a 3rd p...

阅读全文>>

评论(0) 浏览(107)

What is a stack trace, and how can I use it to debug my application errors?

2021-6-3 anglehua

Sometimes when I run my application it gives me an error that looks like: Exception in thread "main" java.lang.NullPointerException at com.example.myproject.Book.getTitle(Book.java:16) at com.example.myproject.Author.getBookTitles(Author.java:25) at com.example.myproject.Boot...

阅读全文>>

评论(0) 浏览(100)

Difference between <context:annotation-config> and <context:component-scan>

2021-6-3 anglehua

I'm learning Spring 3 and I don't seem to grasp the functionality behind <context:annotation-config> and <context:component-scan>. From what I've read they seem to handle different annotations (@Required, @Autowired etc vs @Component, @Repository, @Service etc), but also from what I've r...

阅读全文>>

评论(0) 浏览(121)

How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?

2021-6-3 anglehua

The code below gives me the current time. But it does not tell anything about milliseconds. public static String getCurrentTimeStamp() { SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy Date now = new Date(); String strDate = sdfDate.format(now); re...

阅读全文>>

评论(0) 浏览(120)

Why is Java Vector (and Stack) class considered obsolete or deprecated?

2021-6-3 anglehua

Why is Java Vector considered a legacy class, obsolete or deprecated? Isn't its use valid when working with concurrency? And if I don't want to manually synchronize objects and just want to use a thread-safe collection without needing to make fresh copies of the underlying array (as CopyOnWriteArray...

阅读全文>>

评论(0) 浏览(116)

Powered by emlog 京ICP备15036472号-3 sitemap