- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请您帮助解决以下问题:
这是专门为澄清我的问题而定制的示例代码:
File accounts_File = new File("Sample_Folder\\example.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
String str_String = ""; //For storing the input from the file.
fis = new FileInputStream(accounts_File);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0)
{
str_String = dis.readLine(); //Reading the line from the file.
StringTokenizer st = new StringTokenizer(str_String);
while (st.hasMoreTokens())
{
//I need some help here
}
}
fis.close();
bis.close();
dis.close();
输入文件 (example.txt) 如下所示:James>Hector>AUS>25
目标只是扫描并用“Anderson”替换“Hector”。问题是“赫克托”这个名字并不为人所知。唯一已知的是它的位置(在第一个“>”之后)。
注意:如果这样更容易的话,我可以更改输入文件的格式。作为示例,我可以将格式更改为:
名字:詹姆斯
姓氏:赫克托
国家/地区:澳大利亚
年龄:25
*程序现在将扫描关键字“LastName:”并将其后面的所有内容替换为“Anderson”。
预先感谢您的帮助!
非常感谢任何建议。
最佳答案
保留格式,它看起来像一个标准的 csv 文件(逗号分隔值,其中“逗号”是“>”字符)并且 csv 文件易于读写!
由于您无法替换“磁盘上”的文件内容,因此您必须将文件读入内存,在内存中重写所需的更改,然后将其重写到磁盘。
文件的一个简单模型是一个二维字符串数组,文件的每一行都是一行,每个值一列,总共 4 列。姓氏(您要替换的字符串)现在始终位于matrix[lineNumber][1]
。更改每行第二列的值即可完成。
对于生产代码,我将编写一个类来表示一条记录。然后,您的代码可能如下所示:
public void replace(File inputFile, String newName) {
List<Person> persons = new ArrayList<Person>();
// read the file
List<String> lines = readFile(inputFile);
// create a person instance for each line
for (String line:lines) {
String[] values = line.split(">");
persons.add(new Person(values));
}
// change the last name
for (Person person:persons) {
person.setLastName(newName);
}
// store all persons to file
writeFile(inputFile, persons);
}
请注意,实际工作(文件 IO、更改名称)是在不同的方法和类中完成的。这使得替换方法中的主要算法保持可读。
关于java - 如何扫描文件并替换某些短语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4601987/
我们正在使用一个脚本,它会返回以下内容120007CONSULTA DE DEUDA CORRECTA.01TEST我正在尝试从awk中获得 CONSULTA DE DEUDA CORRECTA 的短
我正在使用以下查询: { "_source": [ "title", "bench", "id_", "court",
例如,我有数十亿个短语,我想要将相似的短语聚集在一起。 > strings.to.cluster <- c("Best Toyota dealer in bay area. Drive out wit
我有一个包含大约 1000 个关键字/短语(一到四个字长)的数据库表 - 这个表很少更改,所以我可以将数据提取到更有用的东西中(比如正则表达式?) - 所以这没有找到/基于自然语言处理猜测关键词..
我想确保我从一开始就做对了,但我找不到好的答案,所以我想问一下。 我要做的是以下内容。 以一句话为例:The quick brown fox jumped over the lazy dog"并使用全
我有一个表单,其中有两个单选按钮。 应该从字符串中搜索整个字符串,第二个应该搜索至少一个单词。 例如: 我将搜索“Awesome ideas for startup”。 如果我选中“搜索整个字符串/短
我有... 一组固定关键字和词组(大约 1,000,000 个),例如 birthday、happy new year、vacation等 10 到 500 字之间的一些可变文本。 我想…… 识别文本
有没有办法删除字符串中重复的和连续的单词/短语?例如。 [in]: foo foo bar bar foo bar [out]: foo bar foo bar 我试过这个: >>> s = 'thi
我正在寻找一种在文本中搜索短语或惯用表达的方法,无论时态或可能的介词/副词如何,例如如果我正在寻找 call off我还想找到 My boss called the meeting off. 之类的用
PowerShell 有一些语法特性,我找不到太多相关文档。今天我的问题是 ? {...} 例子 PS> Get-SPServiceInstance | ? {$_.GetType.toString(
我通过 PHP 使用 Solr 来搜索我网站的各个方面。我正在尝试实现一项功能,但找不到任何有关如何实现它的信息。 我有一组文档(评论),每个文档都与特定产品有关。 我想找到出现在单个产品的多条评论中
我正在从事自然语言生成任务,需要检索与 Cyc 术语等效的自然语言单词或短语。例如。 “#$EatingEvent”->“吃”或“#$Coyote-Animal”->“土狼”。 如何通过 Java A
我在使用 Java 中的以下正则表达式时遇到问题。我删除了双引号,以使其更具可读性。由于缺乏更好的词,我还将每个“组件”放在单独的行上(但实际的表达式会将行合并,行之间没有空格): (?:\bIN(?
我有一个 MySQL 数据库,我正在尝试使用 PHP 获取趋势主题(流行短语或单词)。我尝试了一些查询,但似乎没有什么对我有用。 最佳答案 您可以尝试创建一个包含两列的新表:WORD 和 COUNT,
我在 MySql 中有一个包含城市的表。 |---------------------|------------------| | ID | City
我正在处理搜索页面上的用户输入。如果用户选择“所有单词”类型的搜索,那么我会从搜索文本中删除所有 bool 搜索运算符,并在每个真实单词之间添加 ' AND '。在大多数情况下非常简单。但是,我不知道
我使用的语音识别软件给出的结果不是最佳的。 例如:session 返回为 fashion 或 mission。 现在我有一本像这样的字典: matches = { 'session': ['fas
我厌倦了使用 Gimp 无休止地滚动所有字体来不规律地搜索完美字体,所以我想:“为什么不为每种字体构建一个可视的 pangrams 列表”。 因此,我尝试构建一个 php 脚本,该脚本在 .fonts
如何编写一个 RegEx 模式来测试一个字符串是否包含多个具有以下结构的子字符串: "cake.xxx" xxx 是任何东西,但不是“奶酪”、“牛奶”或“黄油”。 例如: “我有一个 cake.hon
我有一个字段“部门”,它是一个列表:{“部门”:[“餐饮服务”,“软件开发”,“制造”,“部署”]}' 我想聚合“部门”中以“d”开头的元素。即,从所有记录进行部署。 我能够找到包含一个前缀为“d”但
我是一名优秀的程序员,十分优秀!