我的文字文件包含:
Hello This is a Test
Press Enter to Continue
我有一个数组:
int StartIndex [] = {1,4,8}
int EndIndex [] = {3,7,11}
String[] VALUES = new String[] {"Sys","Jav","Tes"};
我想用'Sys'替换索引{1,3},用'Jav'替换索引{4,7},等等。
我的想法是将整个文件作为String读取,然后传递索引以替换VALUES字符串。
我该怎么做?
CODE:
String[] VALUES = new String[] {"Sys"}; //Correct Solutions
int [] StartIndex ={4};
int [] EndIndex ={6};
while ((line = br.readLine()) != null) {
// Print the content on the console
System.out.println (line);
StringBuffer buf = new StringBuffer(line);
buf.replace(StartIndex[0], EndIndex[0], VALUES[0]);
done =buf.toString();
System.out.println(done);
预期的输出应该是这样的:
SyslJavhTes is a Test
Press Enter to Continue
我搜索了一下并得到了这个:
String myName = "domanokz";
char[] myNameChars = myName.toCharArray();
myNameChars[4] = 'x';
myName = String.valueOf(myNameChars);
如果我们将文件转换为字符串并应用此功能,这会有效吗?
答案 0 :(得分:1)
问题解决了!代码完美无缺,因为我测试了它。就像之前没有添加任何评论所以你会理解&学习。 (如果其他人能够找到正确的答案,请投票/接受答案)。
<强> CODE:强>
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
*
* @author jtech
*/
public class ReplaceWithIndexes
{
public static void main(String[] args) throws IOException
{
BufferedReader br = null;
boolean endMatched = false;
int startIndex[] = {0,4,8};
int endIndex[] = {3,7,10};
int c = 0, c1 = 0, c2 = 0, largestVal_start = 0, largestVal_end = 0, lineCount = 0;
String line = null, newString = "";
String[] VALUES = new String[] {"Sys","Jav","Tes"};
br = new BufferedReader(new FileReader("C:\\Users\\jtech\\Documents\\NetBeansProjects\\HelpOthers\\src\\textFiles\\AnotherFile.txt"));
for (int i = 0; i < startIndex.length; i++)
{
if (startIndex[i] > largestVal_start)
{
largestVal_start = startIndex[i];
}
}
for (int i = 0; i < endIndex.length; i++)
{
if (endIndex[i] > largestVal_end)
{
largestVal_end = endIndex[i];
}
}
while ((line = br.readLine()) != null)
{
StringBuilder buf = new StringBuilder(line);
// Print the content on the console
System.out.println(line);
lineCount++;
while (c <= largestVal_start)
{
while (c1 <= largestVal_end)
{
if (startIndex[0] == c && endIndex[0] == c1)
{
buf.replace(startIndex[0], endIndex[0], VALUES[c2]);
newString = buf.toString();
endMatched = true;
}
else if (startIndex[1] == c && endIndex[1] == c1)
{
buf.replace(startIndex[1], endIndex[1], VALUES[c2]);
newString = buf.toString();
endMatched = true;
}
else if (startIndex[2] == c && endIndex[2] == c1)
{
buf.replace(startIndex[2], endIndex[2], VALUES[c2]);
newString = buf.toString();
endMatched = true;
}
c1++;
}
for (int i = 0; i < startIndex.length; i++)
{
if (c == startIndex[i])
{
c2++;
}
}
if (endMatched == true || ((c1 <= largestVal_end) == false) )
{
c1 = 0;
endMatched = false;
}
c++;
}
if (lineCount <= 1)
{
System.out.println("Updated line: " + newString);
}
}
}
}
答案 1 :(得分:0)
最简单的解决方案是以下代码,但对于大型文件和/或大量替换,它可能效率不高。
while ((line = br.readLine()) != null) {
// Print the content on the console
System.out.println (line);
StringBuffer buf = new StringBuffer(line);
for (int i = 0; i < VALUES.length; i ++) {
buf = buf.replace(StartIndex[i], EndIndex[i], VALUES[i]);
}
done = buf.toString();
System.out.println(done);
}
答案 2 :(得分:0)
在Java中,这门课程是众所周知的锤子,每一个问题都是钉子。你也需要一个,而不是管理三个独立的数组。
class Replacer {
private final int start, end;
private final String replacement;
Replacer(int start, int end, String replacement) {
this.start = start; this.end = end; this.replacement = replacement;
}
String replace(String in) {
StringBuilder b = new StringBuilder(in);
b.replace(start, end, replacement);
return b.toString();
}
}
然后创建一个替换者列表:
List<Replacer> replacers = Arrays.asList(
new Replacer(1, 3, "System"),
new Replacer(4, 7, "Java"),
new Replacer(8, 11, "Testing")
);
并将它们应用于每一行:
while ((line = br.readLine()) != null) {
for (Replacer r : replacers) line = r.replace(line);
System.out.println(line);
}