回 帖 发 新 帖 刷新版面

主题:在java中如何实现从一个文本文件中读取多个数字,并将其按升序输出到另一文本?已经写出程序,请改错

在java中如何实现从一个文本文件中读取多个数字如(1, ,7,5, ,2,9,3,6,4),其中数字之间有空格,将其按升序输出到另一文件,其中空格不输出,以下程序是我写的,但是有错误,大家帮我看看啊 
错误:FileRW.java:63:非法转义字符
     JTextArea display=new JTextArea("F:\a.text");
大家帮我看看这个程序能不能实现啊

public class FileRW
{
    public FileRW()
    {}
    private void rwTextFile(JTextArea display,String fileName)
    {
        try{
                Scanner in=new Scanner(new FileInputStream(fileName));//读取文件 
                while(in.hasNextLine())//对每行做处理 
                { 
                String line=in.nextLine(); 
                StringTokenizer st=new StringTokenizer(line,",");//用来分开数字和标点符号 
                ArrayList<Integer> l=new ArrayList<Integer>(); 
                while(st.hasMoreTokens())//把一行里的每个字符添加进去 
                { 
                String num=st.nextToken();
                if(num!=null)
                 {
                int temp=Integer.parseInt(num); 
                l.add(temp);
                 }
                } 
                int length=l.size(); 
                int[] arr=new int[length]; 
                for(int i=0;i<length;i++)//把List转成int[]型的数组 
                { 
                arr[i]=l.get(i); 
                } 
                //在这里调用你的排序函数,并直接送到text控件中就行了。
                int t;
                for (int i=0;i<arr.length-1;i++)
                {
                    for(int j=0;j<arr.length-1-i;j++)
                    {
                        if(arr[ j ] > arr[j + 1 ] ) 
                        {
                            t = arr[ j ] ;
                            arr[ j ] = arr[ j + 1 ]; //小的沉底,大的上浮
                            arr[ j + 1 ] = t;
                        }
                    }
                }
            }
        
         

        
                FileWriter out=new FileWriter(fileName);
                out.write(display.getText());
                out.close();
                }
                    
        catch (Exception e){
                        display.setText("Error:"+e.getMessage()+"\n");
                        e.printStackTrace();
            }
        catch (IOException e){
                        display.setText("IOError:"+e.getMessage()+"\n");
                        e.printStackTrace();
                      }
        
           
    }
    public static void main(String[] args) 
    {
        JTextArea display=new JTextArea("F:\a.text");
        FileRW f=new FileRW();
        f.rwTextFile(disply,"F:\b.text");
    }
}

public class FileRW
{
    public FileRW()
    {}
    private void rwTextFile(JTextArea display,String fileName)
    {
        try{
                Scanner in=new Scanner(new FileInputStream(fileName));//读取文件 
                while(in.hasNextLine())//对每行做处理 
                { 
                String line=in.nextLine(); 
                StringTokenizer st=new StringTokenizer(line,",");//用来分开数字和标点符号 
                ArrayList<Integer> l=new ArrayList<Integer>(); 
                while(st.hasMoreTokens())//把一行里的每个字符添加进去 
                { 
                String num=st.nextToken();
                if(num!=null)
                 {
                int temp=Integer.parseInt(num); 
                l.add(temp);
                 }
                } 
                int length=l.size(); 
                int[] arr=new int[length]; 
                for(int i=0;i<length;i++)//把List转成int[]型的数组 
                { 
                arr[i]=l.get(i); 
                } 
                //在这里调用你的排序函数,并直接送到text控件中就行了。
                int t;
                for (int i=0;i<arr.length-1;i++)
                {
                    for(int j=0;j<arr.length-1-i;j++)
                    {
                        if(arr[ j ] > arr[j + 1 ] ) 
                        {
                            t = arr[ j ] ;
                            arr[ j ] = arr[ j + 1 ]; //小的沉底,大的上浮
                            arr[ j + 1 ] = t;
                        }
                    }
                }
            }
        
         

        
                FileWriter out=new FileWriter(fileName);
                out.write(display.getText());
                out.close();
                }
                    
        catch (Exception e){
                        display.setText("Error:"+e.getMessage()+"\n");
                        e.printStackTrace();
            }
        catch (IOException e){
                        display.setText("IOError:"+e.getMessage()+"\n");
                        e.printStackTrace();
                      }
        
           
    }
    public static void main(String[] args) 
    {
        JTextArea display=new JTextArea("F:\a.text");
        FileRW f=new FileRW();
        f.rwTextFile(disply,"F:\b.text");
    }
}

回复列表 (共1个回复)

沙发

把路径中的\换成\\即可。

我来回复

您尚未登录,请登录后再回复。点此登录或注册