怎样将textarea中的内容写入文件,特别是换行的问题。
多谢~~~

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【aaronhanfei】截止到2008-07-03 16:37:04的历史汇总数据(不包括此帖):
    发帖的总数量:6                        发帖的总分数:270                      
    结贴的总数量:6                        结贴的总分数:270                      
    无满意结贴数:1                        无满意结贴分:20                       
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:16.67 %               无满意结分率:7.41  %                  
    敬礼!
      

  2.   

    直接用I/O操作就可以了 关于换行的问题可以replace一下
    /**
       * 在页面上直接显示文本内容,替换小于号,空格,回车,TAB
       * 
       * @param str String 原始字符串
       * @return String 替换后的字符串
       */
      public static String htmlshow(String str) {
        if (str == null) {
          return null;
        }    str = replace("<", "&lt;", str);
        str = replace(" ", "&nbsp;", str);
        str = replace("\r\n", _BR, str);
        str = replace("\n", _BR, str);
        str = replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;", str);
        return str;
      }
      

  3.   


        /**
         * 写至文件
         * @param file 文件对象
         * @param text JTextArea对象
         */
        public void writeFile(File file, JTextArea text)
        {
            String str = formatLineSeparator(text.getText(), "\n", System.getProperty("line.separator"));
            FileOutputStream outFile = null;
            FileChannel channel = null;
            final byte[] bt = str.getBytes();
            ByteBuffer buf = null;        try
            {
                outFile = new FileOutputStream(file);
                channel = outFile.getChannel();
                int size = bt.length / 1024;
                int mod = bt.length % 1024;
                int start = 0;
                int end = 0;
                
                for(int i = 0; i < size + 1; i++)
                {
                    if(i == size)
                    {
                        if(mod > 0)
                        {
                            buf = ByteBuffer.allocate(mod);
                            start = end;
                            end += mod;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        buf = ByteBuffer.allocate(1024);
                        start = end;
                        end = (i + 1) * 1024;
                    }
                    
                    for(byte b: getSubBytes(bt, start, end))
                    {
                        buf.put(b);
                    }                buf.flip();
                    channel.write(buf);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    channel.close();
                    outFile.close();
                }
                catch(Exception e)
                {}
            }
        }
        
        /**
         * 获取一字节数组的一子数组
         * @param bt 基准字节数组
         * @param start 开始位置
         * @param end 结束位置
         * @return 子字节数组
         */
        private byte[] getSubBytes(byte[] bt, int start, int end)
        {
            int size = end - start;
            byte[] result = new byte[size];
            
            for(int i = 0; i < size; i++)
            {
                result[i] = bt[i + start];
            }
            
            return result;
        }
        
        /**
         * 格式化换行符
         * @param str 文本
         * @param oldLineSep 旧的换行符
         * @param newLineSep 新的换行符
         * @return 格式化后的文本
         */
        private String formatLineSeparator(String str, String oldLineSep, String newLineSep)
        {
            StringBuffer sb = new StringBuffer(str);
            int length = oldLineSep.length();
            long index = -1;
            String tempStr = str;
            
            while((index = tempStr.lastIndexOf(oldLineSep)) >= 0)
            {
                tempStr = tempStr.substring(0, (int)index);
                sb.delete((int)index, (int)(index + length));
                sb.insert((int)index, newLineSep);
            }
            
            return sb.toString();
        }
      

  4.   

    以上是我以前写的Notepad里用到的,效率不错~~~~~~~~~~~