转贴zm1313的专栏一个文件处理类DealString.java

DealString.java
 
一、说明:
       1. 一个主要负责字符串处理的类
 
二、变量:
序号
变量名
应用范围
变量类型
初始值
是否static
作用
1
infile
Package
String
“”
输入文件
2
outfile
Package
String
“”
输出文件
3
fis
Package
FileInputStream
Null
输入文件流
4
fos
Package
FileOutputStream
Null
输出文件流
三、方法简介:
序号
方法名
参数
返回值
功能
1
connFIS
String strFile
void
建立文件输入流, 通过参数中指定的文件路径
fis = new FileInputStream(infile);
2
connFOS
String strFile
void
建立文件输出流, 通过参数中指定的文件路径
fos = new FileOutputStream(outfile);
3
closeFIS
Void
关闭输入文件流
fis.close()
4
closeFOS
Void
关闭输出文件流
fos.close()
5
getFIS
FileInputStream
取得输入流
return fis
6
getFOS
FileOutputStream
取得输出流
return fos;
7
deleteFile
Strign strFile
Void
删除文件, strFile 指字的文件
8
movefile_FileStream
 
Void
拷贝文件内容fis->fos
9
movefile_BufferedByteStream
 
void
拷贝文件内容fis->fos
10
movefile_BufferedCharStream
 
Void
拷贝文件内容infile->outfile
11
readCHStr
 
string
读中文字符串 从infile->
12
readCHStr
InputStream is
String
读中文字符串 从inputstream
 
13
toInputStream
String str
FileInputStream
将字符串转换为数据流
14
writeCHStr
String str
Void
写中文字符串到文件->outfile
15
writeCHStr
OutputStream os
String str
Void
写中文字符串到文件->outfile
16
appendCHStr
String outfile
String str
Void
追加到文件结尾
17
seekStrPos
long cur
String str
Long
从位置cur开始检索第一个str的位置
18
seekStrPos
String str
long
定位某一个字符串在文件中的位置(左起)
19
seekStrPos
String str1
String str2
Long
定位从字符串str1开始第一个字符串str2的位置
20
substring
long pos
int len
String
从pos位置开始去长度为len的字符串
21
substring
String str1
String str2
int len
String
从字符串str1开始检索str2后的长度为len的字符串
四、原码

package oa.main;
import java.io.*;
public class DealFile
{
    /**输入文件*/
    String infile = ""
    /**输出文件*/
    String outfile = "";
    /**输入文件流*/
    FileInputStream fis = null;
    /**输出文件流*/
    FileOutputStream fos = null;
    public DealFile()
    {
    }
    /**
     * 建立输入文件流
     * */
    public void connFIS(String i)
    {
        try{
            infile = i;
            fis = new FileInputStream(infile);
        }catch(IOException ioe){
            System.out.println("调用DealFile.connFIS()函数错误:/r/n"+ioe);
        }
    }
    /**
      * 建立输出文件流
      */
    public void connFOS(String o)
    {
        try{
            outfile = o;
            fos = new FileOutputStream(outfile);
        }catch(IOException ioe){
            System.out.println("调用DealFile.connFOS()函数错误:/r/n"+ioe);
        }
    }
    /**
     * 关闭输入文件流
     * */
    public void closeFIS()
    {
        try{
            if (fis != null )
            {
                fis.close();
            }
        }catch(IOException ioe){
            System.out.println("调用DealFile.closeFIS()函数错误:/r/n"+ioe);
        }
    }
    /**
     * 关闭输出文件流
     * */
    public void closeFOS()
    {
        try{
            if( fos != null)
            {
                fos.close();
            }
        }catch(IOException ioe){
            System.out.println("调用DealFile.closeFOS()函数错误:/r/n"+ioe);
        }
    }

    /**
     * 取得输入流
     * */
    public FileInputStream getFIS()
    {
        return fis;
    }
    /**
     * 取得输出流
     * */

    public FileOutputStream getFOS()
    {
        return fos;
    }
    /**
     * 删除文件
     * */
    public void deleteFile(String df)
    {
        File file = new File(df);
        file.delete();  
    }
    /**
     * 拷贝文件内容fis->fos
     * */
    public void movefile_FileStream()
    {
        try{
            File f = new File(infile);
            byte b[] = new byte[ (int) ( f.length() ) ];
            fis.read(b);
            fos.write(b);
        }catch(IOException ioe){
            System.out.println("调用DealFile.movefile_FileStream()函数错误:/r/n"+ioe);
        }
    }
    /**
     * 拷贝文件内容fis->fos
     * */

    public void movefile_BufferedByteStream()
    {
        try{
            BufferedInputStream in = new BufferedInputStream(fis);
            BufferedOutputStream out = new BufferedOutputStream(fos);
            int c;
            while( (c = in.read()) != -1)
            {
                out.write(c);
            }
            in.close();
            out.close();
        }catch(IOException ioe){
            System.out.println("调用DealFile.movefile_BufferedByteStream()函数错误:/r/n"+ioe);
        }
    }
    /**
     * 拷贝文件内容infile->outfile
     * */

    public void movefile_BufferedCharStream()
    {
        try{
            BufferedReader in = new BufferedReader(new FileReader(infile));
            BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
            int c;
            while(( c = in.read()) != -1)
            {
                out.write(c);
            }
            in.close();
            out.close();
        }catch(IOException ioe){
            System.out.println("调用DealFile.movefile_BufferedCharStream()函数错误:/r/n"+ioe);
        }
    }
    /**
     * 读中文字符串infile->
     * */
    public String readCHStr()
    {
        return readCHStr(fis);
    }
    /**
     * 读中文字符串infile->
     * */
    public String readCHStr(InputStream is)
    {
        String str = "";
        try{
            // 建立Unicode字符流
            InputStreamReader isw = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isw);
            // 读Unicode字符串
            String s = "";
            while((s = br.readLine()) != null)
            {
                if( !str.equals("") )
                {
                    str = str + "/r/n" + s;
                }else {
                    str = str + s;
                }
            }
            br.close();
            isw.close();
        }catch(IOException ioe){
            System.out.println("调用DealFile.readCHStr()函数错误:/r/n"+ioe);
        }
        return str;
    }
    /**
     * 将字符串转换为数据流
     * */
    public FileInputStream toInputStream(String str)
    {
        FileInputStream fis_t = null;
        try{
            // 将字符串写入临时文件,再从文件生成数据流
            FileOutputStream fos_t = new FileOutputStream("tmp.txt");
            writeCHStr(fos_t, str);
            fis_t = new FileInputStream("tmp.txt");
        }catch(IOException ioe){
            System.out.println("调用DealFile.toInputStream()函数错误:/r/n"+ioe);
        }
        return fis_t;
    }
    /**
     * 写中文字符串到文件->outfile
     * */
    public void writeCHStr(String str)
    {
        writeCHStr(fos, str);
    }
    /**
     * 写中文字符串到文件->outfile
     * */

    public void writeCHStr(OutputStream os, String str)
    {
        try{
            //建立Unicode字符流
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            //写Unicode字符串
            bw.write(str, 0, str.length());
            bw.newLine();
            bw.close();
            osw.close();
        }catch(IOException ioe){
            System.out.println("调用DealFile.writeCHStr()函数错误:/r/n"+ioe);
        }
    }
    /**
     * 追加到文件结尾
     * */

    public void appendCHStr(String outfile, String str)
    {
        try{
            RandomAccessFile rf = new RandomAccessFile(outfile, "rw");
            rf.seek( rf.length() );
            rf.writeBytes(str);
            rf.close();
        }catch(IOException ioe){
            System.out.println("调用DealFile.appendCHStr()函数错误:/r/n"+ioe);}
        }
    /**
     * 从位置cur开始检索第一个str的位置
     * */

    public long seekStrPos(long cur,String str)
    {
        long fcur = cur;
        try{
            RandomAccessFile file = new RandomAccessFile(new File(infile),"r");   
            long flen = file.length();
            int slen = str.length();
            byte b[] = new byte[slen];
   
            for(; fcur < flen ; fcur++)
            {
                file.seek(fcur);
                // 文件尾剩余长度不再够时
                if(( flen-fcur ) < slen)
                {
                    fcur = -1;
                    break;
                }
                //判断当前位置读取的字符串是否与参数字符串匹配,不是则继续搜索
                file.read(b, 0, slen);
                String bstr = new String( b );
                if( str.equals( bstr ) )
                {
                    break;
                }
            }
            file.close();
        }catch(IOException ioe){
            System.out.println("调用DealFile.seekStrPos()函数错误:/r/n"+ioe);
        }
        return fcur;
    }
    /**
     * 在文件中定位某一个字符串在文件中的位置(左起)
     * */

    public long seekStrPos(String str)
    {
        return seekStrPos(0, str);
    }
    /**
     * 在文件中定位从字符串str1开始, 第一个字符串str2的位置
     * */
    public long seekStrPos(String str1,String str2)
    {
        long cur = seekStrPos(0, str1);
        return seekStrPos(cur , str2);
    } 
    /**
     * 从文件的pos位置开始, 取长度为len的字符串
    * */
    public String substring(long pos, int len)
    {
        String str = "";
        try{
            RandomAccessFile file = new RandomAccessFile(new File(infile),"r");
            long flen = file.length();
            //当不能返回时返回空值
            if(pos < 0 || ( flen - pos ) < len)
            {
                return "";
            }
            file.seek(pos);
            byte b[] = new byte[len];
            file.read(b, 0 , len);
            str = new String( b );
            file.close();
        }catch(IOException ioe){
            System.out.println("调用DealFile.substring()函数错误:/r/n"+ioe);
        }
        return str;
    }
    /**
     * 从字符串str1开始 , 检索str2后, 长度为len的字符串
     * */
    public String substring(String str1,String str2,int len)
    {
        long pos = seekStrPos(str1, str2);
        return substring(pos + str2.length(), len).trim();
    }
    public static void main(String args[]) throws IOException,ArrayIndexOutOfBoundsException
    {
        DealFile df = new DealFile();
  
        //*追加文件方法(一旦作为输出文件被打开,即被清空)
        //df.connFIS("out.txt");
        //String str = df.readCHStr()+"测试追加WWWWWWWWTTTTW我的还";
  
        //df.connFOS("out.txt");
        //df.writeCHStr(str);
        //df.closeFIS();
        //df.closeFOS();
        //拷贝文件
        df.connFIS("out.txt");
        df.connFOS("in.txt");
        df.movefile_FileStream();
        //df.movefile_BufferedByteStream();
        //df.movefile_BufferedCharStream();
        df.closeFIS();
        df.closeFOS();
        //df.connFIS("out.txt");
        //System.out.println(df.substring("flyline","background-color:",7).toUpperCase());
        //df.closeFIS();
    }
}
 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 来表示数据库表,使用的实例表示表中的行。 开发者可以定义之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值