Java编程中如何读取键盘输入
导入java.util.Scanner;使用Scanner来接收;例如:import java.util.Scanner;public class AAA { public static void main(String[] args) { Scanner scan=new Scanner(System.in); String str=scan.next(); System.out.println(str); }}执行时,在控制台输入字符串,会将输入的数据打印出来
java读取文件流乱码输出乱码
如果你全是中文的话,中文是两个字节,那么可以采用两个字节一起读,如果你中英文交叉的文本,那就不好办了,只能把文本一次性全都读进来再输出。纯中文读取:File f1 = new File ("a.txt"); FileInputStream is=new FileInputStream(f1); int i; byte[] b =new byte[2]; while((i=is.read(b))!=-1){ System.out.print(new String(b)); } 中英文混合的文本读取 byte[] b =new byte[yourtxtlength]; yourtxtlength就是你文本字节的长度
java读写CSV文件的方法
可以通过流的形式读取到所有内容,之后在转换成元素的形式进行实现。举例:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
public class Test{
public static void main(String[] args) {
Hashtable<String, String[]> dict = new Hashtable<String, String[]>();
try {
BufferedReader reader = new BufferedReader(new FileReader("test.csv"));
String line = null;
while((line=reader.readLine())!=null){
String item[] = line.split(",");
String item2[] = new String[19];
System.arraycopy(item,1,item2,0,19);
dict.put(item[0],item2);
}
Enumeration e2 = dict.keys();
while (e2.hasMoreElements()) {
String key = (String) e2.nextElement();
System.out.println(key);
String[] dd = (String[])dict.get(key);
for (int i=0;i<dd.length;i++) {
System.out.print(dd[i]+"\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}

