public class Read {
public static void main(String[] args) {
System.out.println(doRead());
}
public static String doRead() {
String result = "";
try {
BufferedReader fr;
InputStream inputStream = Read.class.getResourceAsStream("/data.txt");
if (inputStream != null) {
fr = new BufferedReader(new InputStreamReader(inputStream));
} else {
fr = new BufferedReader(new FileReader(new File("resources/data.txt")));
}
result = fr.readLine();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
Andy's Java notes
Tuesday, 23 August 2011
Reading Files cross jars
To my somewhat annoyance I discovered that reading files from the current package is done with a normal FileReader but if you want to read the same file from a different jar you have to use the getResourceAsStream() method.
Tuesday, 15 March 2011
Wednesday, 2 February 2011
SVN revert!
Tortoise:
Undo changes:
Tortoise SVN -> update to revision -> select revision -> RIGHT click -> revert to this revision.
Undo 1 change
Tortoise SVN -> Merge -> Reverse Merge (tick) -> select revision to remove
Command line:
Reverse Merge
Situation: Trunk was good upto 497 and now its all gone wrong. To revert everything and set the HEAD back to 497 do:
svn merge -r HEAD:497 .
Undo changes:
Tortoise SVN -> update to revision -> select revision -> RIGHT click -> revert to this revision.
Undo 1 change
Tortoise SVN -> Merge -> Reverse Merge (tick) -> select revision to remove
Command line:
Reverse Merge
Situation: Trunk was good upto 497 and now its all gone wrong. To revert everything and set the HEAD back to 497 do:
svn merge -r HEAD:497 .
Wednesday, 22 December 2010
Debug - Heap and memory allocation
So Java comes with these tools now:
But there is also this neat little tool allowing you to look inside the heap and see how the memory is allocated
Free simple profiler - JIP
'Heavy' profiling
jconsole
jvisualvm
But there is also this neat little tool allowing you to look inside the heap and see how the memory is allocated
jmap -heap <process id>
Free simple profiler - JIP
'Heavy' profiling
Friday, 26 November 2010
Collections and ConcurrentModificationExceptions
Unmodifiable collections can STILL throw ConcurrentModificationExceptions
Please remember this and use CopyOnWriteArrayList instead.
Please remember this and use CopyOnWriteArrayList instead.
Tuesday, 23 November 2010
Log4j to different files
How to log to different files based on packages.
Sample log4j.properties file:
Sample log4j.properties file:
log4j.rootLogger=WARN,
#Overriding log levels in certain packages:
log4j.logger = INFO, all
log4j.logger.com.drkw.swaptradesubscriber=INFO, tradeReadingFile
log4j.logger.com.drkw.tibco=INFO, tibcoMessages
# For stdout use this one:
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.tibcoMessages=org.apache.log4j.FileAppender
log4j.appender.tibcoMessages.File=C:/temp/tradeSubscriber-tibco.log
log4j.appender.tibcoMessages.layout=org.apache.log4j.PatternLayout
log4j.appender.tibcoMessages.layout.ConversionPattern=%d{ISO8601} %p %t %c - %m%n
log4j.appender.tibcoMessages.Append=false
log4j.appender.tibcoMessages.Threshold=DEBUG
log4j.appender.tradeReadingFile=org.apache.log4j.FileAppender
log4j.appender.tradeReadingFile.File=C:/temp/tradeSubscriber-load.log
log4j.appender.tradeReadingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.tradeReadingFile.layout.ConversionPattern=%d{ISO8601} %p %t %c - %m%n
log4j.appender.tradeReadingFile.Append=false
log4j.appender.all=org.apache.log4j.FileAppender
log4j.appender.all.File=C:/temp/tradeSubscriber-all.log
log4j.appender.all.layout=org.apache.log4j.PatternLayout
log4j.appender.all.layout.ConversionPattern=%d{ISO8601} %p %t %c - %m%n
log4j.appender.all.Append=false
Wednesday, 13 October 2010
Multi-threading with JMock
A way to make jmock 'pause' when running a test:
http://www.jmock.org/threading-scheduler.html
http://www.jmock.org/threading-scheduler.html
DeterministicScheduler scheduler = new DeterministicScheduler();
scheduler.tick(500, TimeUnit.MILLISECONDS);
Subscribe to:
Posts (Atom)