62,635
社区成员




System.out.println("the file");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\file"+i+".txt"));
String s =null;
StringBuilder sb=new StringBuilder();
while((s=br.readLine())!=null)
{
System.out.println(s);
if(s!=null)
{
sb.append(s+'\r');
}
}
String temp = sb.toString();
String[] array=temp.split("\\r");
String[] s1=array[0].split("\\s+");
String[] s2=array[1].split("\\s+");
for(int k=0;k<s1.length;k++)
{
System.out.println(s1[i].equals(s2[i]));
}
} catch (IOException e) {
e.printStackTrace();
}
看看这个行吗?import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestString2Array {
public static void main(String[] args) {
Integer[][] a = getArrays();
for (Integer[] i : a) {
for (int j : i) {
System.out.print(j + " ");
}
System.out.println();
}
}
public static Integer[][] getArrays() {
Scanner scan = new Scanner(System.in);
List<Integer[]> list = new ArrayList<Integer[]>();
while (scan.hasNext()) {
String line = scan.nextLine();
if (line.equals("-1")) {
break;
}
Scanner lineScan = new Scanner(line);
List<Integer> lineList = new ArrayList<Integer>();
while (lineScan.hasNext()) {
int i = lineScan.nextInt();
lineList.add(i);
}
list.add(lineList.toArray(new Integer[lineList.size()]));
}
Integer[][] array = new Integer[list.size()][];
list.toArray(array);
return array;
}
}
System.out.println("the file");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\file"+i+".txt"));
String s = null;
String ss = null;
while((s=br.readLine())!= null){
System.out.println(s);
}
// BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\file"+i+".txt"));
// String s = null;
while((ss=br.readLine())!= null){
System.out.println(ss);
/* for(int j = 0; j < s.length(); j ++){
String [] arr = s.split("\\s+");
System.out.print(arr[j]);*/
}
}
文件的内容是
A 1 2
B 2 4
现在有个问题就是,s可以输出文件的内容,但是ss输出的内容是空。这个有点不明白
我想实现的是,第一行的三个元素(A, 1, 2)分别和第二行的三个元素(B, 2, 4)。 也就是 A和B比较是否相等,1 和2 比较, 2 和4 比较。 想了好久都没有想到改怎么实现。
这两个是不同的步骤。先输出文件的内容,然后再比较各个元素。
谢谢了[/quote]
ss不输出的原因是br的文件指针已经到达文件的末尾。所以你的ss的值一直未null,所以不会打印。
public static void main(String[] args) {
System.out.println("the file");
Scanner scanner = new Scanner(System.in);
// 为-1时,结束文件打印
int i;
while (-1 != (i = scanner.nextInt())) {
printFile(i);
}
}
private static void printFile(int i) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream((new File(
"C:\\Users\\Administrator\\Desktop\\file" + i + ".txt")))));
String s = null;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("the file");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\file"+i+".txt"));
String s = null;
String ss = null;
while((s=br.readLine())!= null){
System.out.println(s);
}
// BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\file"+i+".txt"));
// String s = null;
while((ss=br.readLine())!= null){
System.out.println(ss);
/* for(int j = 0; j < s.length(); j ++){
String [] arr = s.split("\\s+");
System.out.print(arr[j]);*/
}
}
文件的内容是
A 1 2
B 2 4
现在有个问题就是,s可以输出文件的内容,但是ss输出的内容是空。这个有点不明白
我想实现的是,第一行的三个元素(A, 1, 2)分别和第二行的三个元素(B, 2, 4)。 也就是 A和B比较是否相等,1 和2 比较, 2 和4 比较。 想了好久都没有想到改怎么实现。
这两个是不同的步骤。先输出文件的内容,然后再比较各个元素。
谢谢了 System.out.println("the file");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\file1.txt"));
String s = null;
while((s=br.readLine())!= null){
// String [] arr = s.split("\\s+");
System.out.println(s);
我想实现 输入的i和FileReader的文件一致, 一个i对应一个fileReader的file,你说的我还是有点不太清楚。
还有就是如何把String转变成一个二维数组。想把输入的文件的每一行和每一列放在一个二维数组里,方便以后的条用。
比如
A 1 2
B 3 4
想实现 arr[0][0] = A, arr[1][0] = B
这个又该如何实现呢,谢谢啦~