替换文件中的单词(基于位置/长度/行)



我想写一个java代码,应该允许用空格替换文件中的单词(以位置,长度和行为特征)。

我写了一个代码来定位要替换的单词。但是我怎么替换这个词呢?我应该复制并写入另一个文件吗?

我知道如何替换"字符串行"中的单词,但我不会替换文件中的单词。下面是当前代码:

File file = new File("myFile");    
FileReader fr = new FileReader(file);  
BufferedReader br = new BufferedReader(fr);  
String line;

int position = 4 ;
int length = 10 ;  
int line_number = 1 ; 
String word = "" ;
int n=0 ;
int line_x = 1 ;

while((line = br.readLine()) != null)
{
if(line_x==line_number)
{
for(int i=0 ;i<line.length();i++)
{
while(i==position)
{
if (n<length)
{
word = word + line.charAt(i+n)  ;
}
else
{
break;
// Replace the word in the file with spaces
}
n++;
}   
} 

}
line_x++ ;
}

fr.close();  

你试过使用filewriter吗?

https://www.geeksforgeeks.org/filewriter-class-in-java/

相关内容

最新更新