4.如何将如下的逗号分隔的string分裂(split)到string数组中 abc,ddfasd,adsdf,dsadf,adfsdf,sdfsdf => String 中的一个split函数 String a = "a,b,c,d,e"; String[] b = a.split(",");
其他的几个别人都贴了,我就贴环境变量吧。 环境变量是系统相关的,在2000/xp下面要执行cmd.exe /c set 再win98下面要执行command.com /c set,再unix下面要执行env命令才能得到所有环境变量 下面是代码: public static Properties getEnvVars() { Process p = null; Properties envVars = new Properties(); try { Runtime r = Runtime.getRuntime(); String OS = System.getProperty("os.name").toLowerCase(); // System.out.println(OS); if (OS.indexOf("windows 9") > -1) { p = r.exec( "command.com /c set" ); } else if ( (OS.indexOf("nt") > -1) || (OS.indexOf("windows 2000") > -1 || (OS.indexOf("windows xp") > -1) ) ) { // thanks to JuanFran for the xp fix! p = r.exec( "cmd.exe /c set" ); } else { // our last hope, we assume Unix (thanks to H. Ware for the fix) p = r.exec( "env" ); } BufferedReader br = new BufferedReader ( new InputStreamReader( p.getInputStream() ) ); String line; while( (line = br.readLine()) != null ) { int idx = line.indexOf( '=' ); String key = line.substring( 0, idx ); String value = line.substring( idx+1 ); envVars.setProperty( key, value ); System.out.println( key + " = " + value ); } } catch (Exception e) { e.printStackTrace(); // we do not care here. Just no env vars for the user. Sorry. } return envVars; }
if(name.exists())
return;
else
File fileName=new File(name);
try
{ FileWriter output=new FileWriter(name);
}
catch(IOException ioException)
{
JOptionPane.showMessageDialog(this,
"Error Open file","Error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
String s4 = ("a,b,c,d,e,")
st = new StringTokenizer(s4, ",");
while(st.hasMoreTokens()){
out.print(st.nextToken();
}
JTextArea outputArea;
File name=new File(szFileName);
BufferedReader input=new BufferedReader(new FileReader(name));
StringBuffer buffer=new StringBuffer();
String text;
while((text=input.readLine())!=null)
buffer.append(text+"\n");
outputArea.append(buffer.toString());
4String strings=new String("abc,ddfasd,adsdf,dsadf,adfsdf,sdfsdf");
StringTokenizer tokens=new StringTokenizer(strings,",");
int i=tokens.countTokens();
String array=new String[i];
int j=0;
while(tokens.hasMoreTokens())
{
array[j]=(String)tokens.nextToken());
j++;
}菜鸟的问题菜鸟解决^_^
abc,ddfasd,adsdf,dsadf,adfsdf,sdfsdf
--------------------------import java.util.*;class StringDivide { public StringDivide(String string,String delimiters) {
this.string = string;
this.delimiters = delimiters;
} public static void main(String[] args) {
StringDivide s = new StringDivide("abc,ddfasd,adsdf,dsadf,adfsdf,sdfsdf",",");
StringTokenizer st = new StringTokenizer(s.string,s.delimiters);
while(st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
} private String string;
private String delimiters;
}
abc,ddfasd,adsdf,dsadf,adfsdf,sdfsdf
=>
String 中的一个split函数
String a = "a,b,c,d,e";
String[] b = a.split(",");
环境变量是系统相关的,在2000/xp下面要执行cmd.exe /c set 再win98下面要执行command.com /c set,再unix下面要执行env命令才能得到所有环境变量
下面是代码:
public static Properties getEnvVars() {
Process p = null;
Properties envVars = new Properties();
try {
Runtime r = Runtime.getRuntime();
String OS = System.getProperty("os.name").toLowerCase();
// System.out.println(OS);
if (OS.indexOf("windows 9") > -1) {
p = r.exec( "command.com /c set" );
}
else if ( (OS.indexOf("nt") > -1)
|| (OS.indexOf("windows 2000") > -1
|| (OS.indexOf("windows xp") > -1) ) ) {
// thanks to JuanFran for the xp fix!
p = r.exec( "cmd.exe /c set" );
}
else {
// our last hope, we assume Unix (thanks to H. Ware for the fix)
p = r.exec( "env" );
}
BufferedReader br = new BufferedReader
( new InputStreamReader( p.getInputStream() ) );
String line;
while( (line = br.readLine()) != null ) {
int idx = line.indexOf( '=' );
String key = line.substring( 0, idx );
String value = line.substring( idx+1 );
envVars.setProperty( key, value );
System.out.println( key + " = " + value );
}
}
catch (Exception e) {
e.printStackTrace();
// we do not care here. Just no env vars for the user. Sorry.
}
return envVars;
}
path和classpath有什么区别??^_^
关于问题1, 其实我只是想得到某个已知名字的环境变量的值, java中应该有某个class的某个方法有和c语言中的getenv()函数功能相同吧? 还是非常感谢CoolAbu的精彩回答,让我知道如何得到系统所有环境变量的值. 其他的回答我也很满意.
有谁知道java中的getenv()吗? 有答案我就马上加分结贴啦.:)
System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("java.class.version"));
System.out.println(System.getProperty("java.class.path"));
System.out.println(System.getProperty("java.io.tmpdir"));
System.out.println(System.getProperty("java.compiler"));
System.out.println(System.getProperty("java.ext.dirs"));
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("os.arch"));
System.out.println(System.getProperty("os.version"));
System.out.println(System.getProperty("file.separator"));
System.out.println(System.getProperty("path.separator"));
System.out.println(System.getProperty("line.separator"));
System.out.println(System.getProperty("user.name"));
System.out.println(System.getProperty("user.home"));
System.out.println(System.getProperty("user.dir"));