结对项目https://github.com/bxoing1994/test/blob/master/源代码
所选项目名称:文本替换 结对人:曲承玉
github地址 :https://github.com/bxoing1994/test/blob/master/源代码
结对人github地址:https://github.com/quchengyu/jiedui/tree/quchengyu-patch-1
用一个新字符串替换文本文件中所有出现每个字符串的地方。文件名和字符串都作为命令行参数进行传递。给出相应的测试文件和测试字符串。
项目设计方案
一起选定项目敲定大体结构后,我负责测试和修改,搭档负责写的代码
首先,需要定义一个命令把文本文档读入内存,并进行异常处理;然后定义一个写数据流,以便于替换;最后将内存中修改后的内容写入文本文档。替换文本中的字符串用 cont = cont.replaceAll("private", "public");可替换全部字符串。

核心算法详细设计
- 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");
}
- 2. 写数据流
同读数据流一样定义一个disk,再定义一个变量cont存储操作值,最后刷新文件,关闭文件。
- 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/源代码的更多相关文章
- https://github.com/chenghuige/tensorflow-exp/blob/master/examples/sparse-tensor-classification/
https://github.com/chenghuige/tensorflow-exp/blob/master/examples/sparse-tensor-classification/ ...
- 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 ...
- https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go
https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go
- 结对项目 https://github.com/quchengyu/jiedui/tree/quchengyu-patch-1
所选项目名称:文本替换 结对人:傅艺伟 github地址 : https://github.com/quchengyu/jiedui/tree/quchengyu-patch-1 用一个新字 ...
- 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/ ...
- 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 ...
- 用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 ...
- GC 的认识(转) https://github.com/qcrao/Go-Questions/blob/master/GC/GC.md#1-什么是-gc有什么作用
1. 什么是 GC,有什么作用? GC,全称 Garbage Collection,即垃圾回收,是一种自动内存管理的机制. 当程序向操作系统申请的内存不再需要时,垃圾回收主动将其回收并供其他代码进行内 ...
- go语言的一个gui 开源 项目 https://github.com/andlabs/ui
go语言的一个gui 开源 项目 https://github.com/andlabs/ui 1 安装 mingw-w64 链接地址: http://mingw-w64.sourceforge. ...
随机推荐
- java news website
http://javacrawl.com/ https://dzone.com/java-jdk-development-tutorials-tools-news https://www.infowo ...
- JdbcTemplate介绍<二>
引言 如果说JdbcTemplate类是Spring Jdbc的核心类,那么execute方法算得上Spring Jdbc的核心方法了,毕竟JdbcTemplate的很多public方法内部实际上是调 ...
- 用js实现随机选取10–100之间的10个数字,存入一个数组,并排序
var iArray = []; function getRandom(istart, iend) { var iChoice = iend - istart + 1; //加1是为了取到100 va ...
- Appium1.9.1 之 Desired Capabilities 释疑
服务关键字 Desired Capabilities在启动session的时候是必须提供的. Desired Capabilities本质上是以key value字典的方式存放,客户端将这些键值对发给 ...
- oracle语句insert into select如何加后续插入条件
oracle语句insert into select如何加后续插入条件 2014-01-21 10:48匿名 分类:其他编程语言 | 浏览 2746 次 oracle中有批量插入语句insert i ...
- C#异步编程のWPF Invoke和BeginInvoke
我们先看个简单的例子: private void Window_Loaded(object sender, RoutedEventArgs e) { Console.WriteLine("B ...
- 【转】wampserver2.5 apache2.4.9配置https 图文
注: 我的wamp环境是2.5版本,apache是2.4.9 装在本地C:wamp 操作系统windows10 1.进入wamp->bin->apache->apache2.4. ...
- js中typeof与instanceof用法小记
今天写JS代码,遇到动态生成多个名称相同的input复选按钮 需要判断其是否是数组,用到了if (typeof(document.MapCheckMgr.checkid)!="undefin ...
- PHP 用 fsockopen()、fputs() 来请求一个 URL,不要求返回
项目需要,场景如下: 某个条件下需要调用接口发送多个请求执行脚本,但是由于每个请求下的脚本执行时间在半个小时左右,所以 就放弃返回执行结果,只要求能秒发送所以就可以. 代码如下: /** * 发起异步 ...
- 深入springboot原理——动手封装一个starter
从上一篇文章<深入springboot原理——一步步分析springboot启动机制(starter机制)> 我们已经知道springboot的起步依赖与自动配置的机制.spring-bo ...