当前位置: 代码迷 >> Java相关 >> stack栈
  详细解决方案

stack栈

热度:418   发布时间:2010-09-07 14:34:08.0
stack栈
如何能将stack中的内容保存到指定的文件中去,这是一个程序片段,我想把System.out.println(stack); // 找到一个结果   不是直接输出,而是输入到test.txt文件中,求帮忙,谢谢
程序代码:
private Stack<Object> stack = new Stack<Object>();
    File f = new File("d:"+File.separator+"test.txt");
    public void getSequence(Object[] arr, int begin, int num) {  
        if (num == 0) {  
           
            System.out.println(stack); // 找到一个结果
            
         } else {  
          // 循环每个可用的元素  
          for (int i = begin; i < arr.length; i++) {  
            
             stack.push(arr[i]);            // 当前位置数据放入结果堆栈
            
           
             swap(arr, begin, i);          // 将当前数据与起始位置数据交换
            
           
             getSequence(arr, begin + 1, num - 1);     // 从下一个位置查找其余的组合
            
            
             swap(arr, begin, i);   // 交换回来
            
             stack.pop();   // 去除当前数据
           }  
         }  
       }  

搜索更多相关的解决方案: stack  

----------------解决方案--------------------------------------------------------
程序代码:

public void saveFile(Stack<Object> stack,File file){//该方法每次将覆盖file文件
        Stack<Object> tempstack=(Stack<Object>) stack.clone();
        FileOutputStream fos=null;
        BufferedWriter writer=null;
        try {
            fos=new FileOutputStream(file);
            writer=new BufferedWriter(new OutputStreamWriter(fos));
            while(!tempstack.isEmpty()){
                Object ob=tempstack.pop();
                writer.write(ob.toString());    //请自己覆盖Object中的toString方法,以便实现输出的String
                writer.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

----------------解决方案--------------------------------------------------------
程序代码:

public void saveFile(Stack<Object> stack,File file){//所有找到的stack都写入同一个文件中
        Stack<Object> tempstack=(Stack<Object>) stack.clone();
        FileWriter fw=null;
        BufferedWriter writer=null;
        try {
            fw=new FileWriter(file, true);
            writer=new BufferedWriter(fw);
            while(!tempstack.isEmpty()){
                Object ob=tempstack.pop();
                writer.write(ob.toString());    //请自己覆盖Object中的toString方法,以便实现输出的String
                writer.newLine();
            }
            writer.newLine();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(fw!=null){
                try {
                    fw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

----------------解决方案--------------------------------------------------------
回复 3楼 shellingford
我自己改了下,解决了,但还是谢谢你
程序代码:
private Stack<Object> stack = new Stack<Object>();
    public void getSequence(Object[] arr, int begin, int num)
            throws FileNotFoundException {
        if (num == 0) {
            //System.out.println("==================");
            
//System.out.println(stack); // 找到一个结果
            sb.append("第"+i+"条"+stack.toString()+"\r\n");
            i++;


        } else {
            // 循环每个可用的元素
            for (int i = begin; i < arr.length; i++) {

                stack.push(arr[i]); // 当前位置数据放入结果堆栈

                swap(arr, begin, i); // 将当前数据与起始位置数据交换

                getSequence(arr, begin + 1, num - 1); // 从下一个位置查找其余的组合

                swap(arr, begin, i); // 交换回来

                stack.pop(); // 去除当前数据
            }

            PrintStream pw = new PrintStream(new File("d:"+File.separator+"abc.txt"));
            pw.append(sb);
            pw.flush();

        }
    }


----------------解决方案--------------------------------------------------------
  相关解决方案