使用独立于系统的换行符 System Independent Newline Characters

Categories: Java; Tagged with: ; @ October 14th, 2010 22:42

这几天在写一段处理’代码’的代码, 过程简单: 读取.java源文件的代码, 处理, 写入目标文件.

为了测试简单, 直接使用’\n’作为换行符, Eclipse控制台打印出来无误. 随后写入文件, Windows记事本打开, 空行变多, 不解, 于是拖入Eclipse查看, 显示无误. 但记事本会无端增加空行若干. 极其不解.

Unix. The ‘\n’ character represents the single Unicode character with the value 10 (‘\u000A’) and is used to separate lines in Unix files. This character is also sometimes called linefeed.

Windows. Windows programs often process a ‘\n’ separated text file correctly (NotePad is a exception), but many expect a pair of characters (carriage return followed by line feed = "\r\n") Use the method below to get the newline string that is appropriate for your system.

Mac. In the past, the Apple Mac requirse lines to be separated by ‘\r’, but their move toward Unix (System X) probably means they also accept ‘\n’. I haven’t used a Mac in quite a while tho, so I’m not positive.

我处理的方式是按照Unix换行符’\n’来处理的, 因此在Windows上不管是输入还是输出都会有问题.

System independent value. You can get the value for the system your Java program is running on from the system properties. It is essential to do this with portable programs, and you should always assume your program is portable, eg, that it might run as an applet or using Webstart.

为了保证可移植性, 务必使用: System.getProperty("line.separator")来取得当前系统的换行符.

public static String newline = System.getProperty("line.separator");

See: http://www.ensta.fr/~diam/java/online/notes-java/io/10file/sys-indep-newline.html

Eclipse is running in a JRE, but a JDK is required 解决方法

Categories: Java; Tagged with: ; @ October 7th, 2010 9:08

安装Maven后每次启动出现警告信息:

Eclipse is running in a JRE, but a JDK is required
Some Maven plugins may not work when importing projects or updating source folders.

分两步解决问题:

1. 检查Eclipse正在使用的JRE

‘Window’ -> ‘Preferences’ -> ‘Java’ -> ‘Installed JREs’ 确定正在使用JDK而非JRE.
image

如果没有JDK, 则先新增一个Standard VM.

2. 配置Eclipse.ini

检查Eclipse配置文件, 增加/编辑以下代码:

-vm
C:\Progra~2\Java\jdk1.6.0_16\jre\bin\javaw

注意事项:

1. 第一行参数名称, 第二行为值, 不能写到同一行中

2. 关于第二行的值, 因为不允许出现空格, 所以使用Progra~1或2 替代”Program Files (x86)”.
如果在Program Files下, 请使用Progra~1, 如果在x86下, 则使用Progra~2

3. 在文件中的位置, 不能放到最后(不能在-vmargs之后), 不放心的直接放到文件最前, 如:

-vm
C:\Progra~2\Java\jdk1.6.0_16\jre\bin\javaw
-startup
.....
--launcher.defaultAction
openFile
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms240m
-Xmx912m

3. 重启Eclipse, 警告不再出现.

See: http://tech.karolzielinski.com/m2eclipse-eclipse-is-running-in-a-jre-but-a-jdk-is-required

JDBC CallableStatement INOUT Parameter 参数使用

Categories: Java; Tagged with: ; @ October 5th, 2010 0:48

INOUT参数既作输入又做输出用.  除了需要使用setDataType(value)之外, 还需要registerOutParameter. 例如:

String sql = "CALL ALTOBJ ( SOMETHING, ?, ?)"; // 第一个参数为INOUT参数, 第二个为OUT 

        CallableStatement proc = connection.prepareCall(sql);
        proc.setInt(1, -1); // 作为IN时, 提供Value
        proc.registerOutParameter(1, Types.INTEGER);  // 作为OUT时 注册输出参数

        proc.registerOutParameter(2, Types.VARCHAR); // 第二个参数为OUT参数, 仅注册.

proc.executeUpdate();

See:

JDBC基础教程之CallableStatement http://www.java-cn.com/club/html/42/n-4942.html

[旧]Java: JDBC 取得ResultSet的长度

Categories: Java; Tagged with: ; @ August 29th, 2010 18:19

JDBC 取得ResultSet的长度:

	resultSet.last(); // 游标移到最后, 获得rs长度
int length = resultSet.getRow();
resultSet.first(); // 还原游标到rs开头

Linux下设置Java Environment(环境变量)

Categories: JavaLinux; Tagged with: ; @ August 5th, 2010 21:47

今天在安装好Java后, 设置了环境变量, 并且source了profile一次, java -version, 测试成功;
后来用putty在外面用的时候发现java变量未生效. vi profile, 文件中变量已设置. 后来才知道每次Source /etc/profile, 只是在当前Terminal内有效… 因此在重启以前, 每次新开的Terminal都需要先source下 profile.

附, 环境变量设置:

#vi /etc/profile

shift+g跳到文件末尾, 追加:

export JAVA_HOME=/usr/lib/jdk
export PATH=$PATH:$JAVA_HOME/bin

Newer Posts <-> Older Posts



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.