所选项目名称:文本替换      结对人:曲承玉

github地址 :https://github.com/bxoing1994/test/blob/master/源代码

结对人github地址:https://github.com/quchengyu/jiedui/tree/quchengyu-patch-1

用一个新字符串替换文本文件中所有出现每个字符串的地方。文件名和字符串都作为命令行参数进行传递。给出相应的测试文件和测试字符串。

项目设计方案

       一起选定项目敲定大体结构后,我负责测试和修改,搭档负责写的代码

首先,需要定义一个命令把文本文档读入内存,并进行异常处理;然后定义一个写数据流,以便于替换;最后将内存中修改后的内容写入文本文档。替换文本中的字符串用     cont = cont.replaceAll("private", "public");可替换全部字符串。

核心算法详细设计

  1. 1.         读数据流

public static String read(File src)

{  StringBuffer res = new StringBuffer();

String line = null;

try

{   BufferedReader reader = new BufferedReader(new FileReader(src));

while ((line = reader.readLine()) != null)

{

res.append(line + "\n");

}

reader.close();

}

定义一个数据流src,定义一个变量line用于存储文件内容,初始为空。读时最好一行一行的读,读一行向内存中写一行,如,

while ((line = reader.readLine()) != null)

{

res.append(line + "\n");

}

  1. 2.  写数据流

同读数据流一样定义一个disk,再定义一个变量cont存储操作值,最后刷新文件,关闭文件。

  1. 3.  异常处理

因为会出现如文件不存在等异常,需要定义异常处理,如,

catch (FileNotFoundException e)

{   e.printStackTrace();  }

catch (IOException e)

{   e.printStackTrace();  }

return res.toString(); }

完整源码

完整源码:给出完整的源代码。如:

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class StringRpl {

public static String read(File src)

{  StringBuffer res = new StringBuffer();

String line = null;

try

{   BufferedReader reader = new BufferedReader(new FileReader(src));

while ((line = reader.readLine()) != null)

{

res.append(line + "\n");

}

reader.close();

}

catch (FileNotFoundException e)

{   e.printStackTrace();  }

catch (IOException e)

{   e.printStackTrace();  }

return res.toString(); }

public static boolean write(String cont, File dist)

{

try

{

BufferedWriter writer = new BufferedWriter(new FileWriter(dist));

writer.write(cont);

writer.flush();

writer.close();

return true;

}

catch (IOException e)

{

e.printStackTrace();

return false;

}

}

/*public StringRpl()

{

}*/

public static void main(String[] args)

{  File src = new File("a.txt");

String cont = StringRpl.read(src);

System.out.println(cont);

//对得到的内容进行处理

cont = cont.replaceAll("private", "public");

System.out.println(cont);  //更新源文件

System.out.println(StringRpl.write(cont, src));

}

}

使用说明与运行结果截图

使用说明:a.txt必须在工作区文件夹下。

运行结果截:运行前a.txt的样式。

运行成功后:

对文本文件的操作,读写数据都要存在,当读的文件较大时数分配的内存必须足够。

心得体会

这次任务让我们更好的懂得了两个人的合作,一个编写代码,一个进行调试与修改。比起一个人做,更效率也更成功

结对项目https://github.com/bxoing1994/test/blob/master/源代码的更多相关文章

  1. https://github.com/chenghuige/tensorflow-exp/blob/master/examples/sparse-tensor-classification/

        https://github.com/chenghuige/tensorflow-exp/blob/master/examples/sparse-tensor-classification/ ...

  2. https://github.com/python/cpython/blob/master/Doc/library/contextlib.rst 被同一个线程多次获取的同步基元组件

    # -*- coding: utf-8 -*- import time from threading import Lock, RLock from datetime import datetime ...

  3. https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go

    https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go

  4. 结对项目 https://github.com/quchengyu/jiedui/tree/quchengyu-patch-1

    所选项目名称:文本替换      结对人:傅艺伟 github地址 : https://github.com/quchengyu/jiedui/tree/quchengyu-patch-1 用一个新字 ...

  5. https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/connections.py

    # Python implementation of the MySQL client-server protocol # http://dev.mysql.com/doc/internals/en/ ...

  6. https://github.com/tensorflow/models/blob/master/research/slim/datasets/preprocess_imagenet_validation_data.py 改编版

    #!/usr/bin/env python # Copyright 2016 Google Inc. All Rights Reserved. # # Licensed under the Apach ...

  7. 用swoole实现mysql的连接池--摘自https://github.com/153734009/doc/blob/master/php/mysql_pool.php

    <?php   $serv = new swoole_server("0.0.0.0", 9508);   $serv->set(['worker_num'=>1 ...

  8. GC 的认识(转) https://github.com/qcrao/Go-Questions/blob/master/GC/GC.md#1-什么是-gc有什么作用

    1. 什么是 GC,有什么作用? GC,全称 Garbage Collection,即垃圾回收,是一种自动内存管理的机制. 当程序向操作系统申请的内存不再需要时,垃圾回收主动将其回收并供其他代码进行内 ...

  9. go语言的一个gui 开源 项目 https://github.com/andlabs/ui

    go语言的一个gui 开源 项目  https://github.com/andlabs/ui 1 安装  mingw-w64  链接地址: http://mingw-w64.sourceforge. ...

随机推荐

  1. Word中类似正则匹配的查找替换通配符的使用详解

    一.Word查找栏代码&通配符一览表 序号 清除使用通配符复选框 勾选使用通配符复选框 特殊字符 代码 特殊字符 代码or通配符 1 任意单个字符 ^? 任意单个字符 ? 2 任意数字 ^# ...

  2. Java设计模式之八 ----- 责任链模式和命令模式

    前言 在上一篇中我们学习了结构型模式的享元模式和代理模式.本篇则来学习下行为型模式的两个模式, 责任链模式(Chain of Responsibility Pattern)和命令模式(Command ...

  3. kuangbin fire搜索bfs

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the ma ...

  4. centos7下安装docker(6镜像总结)

    学了很长时间的镜像了,从镜像的分层,缓存的特性,到制作镜像:通过docker commint和docker build创建,再到制作dockerfile以及dockerfile中常用的参数FROM,M ...

  5. innerHTML、innerText和outerHTML、outerText的区别

    区别描述如下: innerHTML 设置或获取位于对象起始和结束标签内的 HTML outerHTML 设置或获取对象及其内容的 HTML 形式 innerText 设置或获取位于对象起始和结束标签内 ...

  6. 修改CentOS 7.2系统的主机名

    之前使用网上的大部分说法,修改了两个配置文件: /etc/hosts /etc/sysconfig/network 然后,并没有什么卵用. 后来,搜阿里云配置,看到这个办法: 使用“经典网络”类型的E ...

  7. php一致性hash算法

    原理部分转自:https://www.jianshu.com/p/e8fb89bb3a61 基本场景 比如你有 N 个 cache 服务器(后面简称 cache ),那么如何将一个对象 object ...

  8. docker in docker 出现 libltdl.so.7 问题

    # docker ps -adocker: error while loading shared libraries: libltdl.so.7: cannot open shared object ...

  9. openzeppelin-solidity/contracts的代码学习——payment

    payment - A collection of smart contracts that can be used to manage payments through escrow arrange ...

  10. 按行读取.txt文件,并按行写入到新文件中

    package com.test.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Fi ...