Tag Archives: java

Local/Instance/Class variable

Local variable, variable defined inside method, constructor or block. Instance variable, a variable inside class. It can be instantiated. Class variable, inside class, but outside method. Must be static. Only one copy per class. public class MyTest { static String str = “abcd”; // classs variable, only one copy private String a = “123”; //… Read More »

Read file name when using MultiResourceItemReader in Springbatch

Springbatch provides MultiResourceItemReader to allow us to read all files in a folder: <bean id=”filenameReader” class=” org.springframework.batch.item.file.MultiResourceItemReader”> <property name=”resources” value=”file:SpringBatchReadFilename/tmp/*.csv” /> <property name=”delegate” ref=”filenameFlatReader” /> </bean> However, there maybe some cases that we want. Assume we have below 2 files and we want to output the filename in processor or writer. 20160402.csv id,value 4,d 20160403.csv id,value… Read More »

log4j output to different log files

Normally when we load a logger in Java, we do like below: private static Logger logger = LoggerFactory.getLogger(App.class); Or private Logger logger3 = LoggerFactory.getLogger(getClass()); Logger like this will by default load the root logger. Alternatively, we can define our own logger. And we can add appender to self-defined logger. In this way, we can output… Read More »

Define bean in SpringMVC

Today, I tested from scratch to see how to use bean in SpringMVC. The conclusion is that we can either use @Component or xml to define bean. First, I used Intellij to create a HelloWorld SpringMVC project. Then I added a TestClass bean which I hope to get it from container. The project structure is like below:… Read More »

Object.hashCode()

I tried below code: int[] a1 = {1, 2}; int[] a2 = {1, 2}; System.out.println(a1.hashCode()); System.out.println(a2.hashCode()); System.out.println(Arrays.hashCode(a1)); System.out.println(Arrays.hashCode(a2)); The result is: 1356652206 1419746043 994 994 The reason is because hashCode() int[] is not overwritten. It is treated as an object. Read below explanation from javaworld, we know hashCode() for Object is a mapping to memory.… Read More »

Contain function() in hashSet

Today when I tried to solve a problem, I came across a weird problem. Look at below code: public class test { static class Point { int a; public Point(int a) { this.a = a; } } public static void test1() { Point element = new Point(1); HashSet<Point> hs = new HashSet<Point>(); hs.add(element); element.a = 2;… Read More »

Ftp in java

Below code download file from ftp server: package ftp; import org.apache.commons.net.ftp.FTPClient; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import org.apache.commons.net.PrintCommandListener; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPReply; /** * Created by pli on 7/16/2015. */ public class FtpDownloader { FTPClient ftp = null; public FtpDownloader(String host, String user, String pwd) throws Exception { ftp = new FTPClient(); ftp.addProtocolCommandListener(new PrintCommandListener(new… Read More »

Sftp in java

Below code can download/upload from/to sftp server: package ftp; import com.jcraft.jsch.*; import lombok.Data; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.file.*; import java.util.Vector; /** * Created by pli on 7/16/2015. */ @Data public class SftpService { private static Logger logger = LoggerFactory.getLogger(SftpService.class); private String host; private String username; private String password;… Read More »

Mock private variable

Use PowerMock to mock private variable. MyClass.java public class MyClass { private String str; public String getStr(){ return str; } } TestDrive.java public class TestDrive { @Test public void test1() { MyClass myClass = new MyClass(); try { MemberModifier.field(MyClass.class, “str”).set(myClass, “This is a mock string injected by test.”); } catch (IllegalAccessException e) { e.printStackTrace(); }… Read More »

Maven, make a fat jar

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation=”org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”> <mainClass>foo.bar.App</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>