请看代码 该程序是输入一组数据 然后将其保存成文件放在某个盘,注意String fileAdd=input.next()那一行,如果我的方法是next(),那么程序就是对的,如果是input.nextLine,程序就错了,到底是为什么啊!!求大神解答啊!!我看了API文档也搞不懂是怎么回事!package sortsave;/**
*
* @author RaywayJohn
*/import java.util.*;
import java.io.*;
public class SortSave
{ /**
* @该程序是输入一组数据 然后将其保存成文件放在某个盘
*/
public static void main(String[] args)throws IOException
{
int i=0;
System.out.println("您要输入的数据个数为:");
Scanner input=new Scanner(System.in);
int total=input.nextInt();
int []arr=new int[total];
System.out.println("您要输入的数据为:");
for(i=0;i<total;i++)
{
arr[i]=input.nextInt();
}
System.out.print("初始数据为:");
for(i=0;i<arr.length;i++)
{
System.out.print(arr[i]+"\t");
}
System.out.print("\n整理后的数据为:");
Arrays.sort(arr);
for(i=0;i<arr.length;i++)
{
System.out.print(arr[i]+"\t");
}
System.out.println("\n请输入你要保存的路径:(例如:X:\\文件名.txt)");
String fileAdd=input.next();
FileWriter fw=new FileWriter(fileAdd);
BufferedWriter bw=new BufferedWriter(fw);
bw.write("整理后的数据为:");
for(i=0;i<arr.length;i++)
{
bw.write(arr[i]+"\t");
}
System.out.println("文件已保存在"+fileAdd+"!");
bw.close();
fw.close();
}}
*
* @author RaywayJohn
*/import java.util.*;
import java.io.*;
public class SortSave
{ /**
* @该程序是输入一组数据 然后将其保存成文件放在某个盘
*/
public static void main(String[] args)throws IOException
{
int i=0;
System.out.println("您要输入的数据个数为:");
Scanner input=new Scanner(System.in);
int total=input.nextInt();
int []arr=new int[total];
System.out.println("您要输入的数据为:");
for(i=0;i<total;i++)
{
arr[i]=input.nextInt();
}
System.out.print("初始数据为:");
for(i=0;i<arr.length;i++)
{
System.out.print(arr[i]+"\t");
}
System.out.print("\n整理后的数据为:");
Arrays.sort(arr);
for(i=0;i<arr.length;i++)
{
System.out.print(arr[i]+"\t");
}
System.out.println("\n请输入你要保存的路径:(例如:X:\\文件名.txt)");
String fileAdd=input.next();
FileWriter fw=new FileWriter(fileAdd);
BufferedWriter bw=new BufferedWriter(fw);
bw.write("整理后的数据为:");
for(i=0;i<arr.length;i++)
{
bw.write(arr[i]+"\t");
}
System.out.println("文件已保存在"+fileAdd+"!");
bw.close();
fw.close();
}}
如果使用nextLine方法,由于当前行为空(还没有输入),所以会抛出异常