在java中,在一定数量的字符后以新行开头
我有一个程序,可以读取一个文件,我可以更改这个文件的内容,然后将它写入另一个文件。输入文件是这样的:输出必须是这样的,我当前的输出是这样的,那么如何更改我的代码,使其在12个字符后开始一行?我还想删除最后一个在java中,在一定数量的字符后以新行开头,java,bufferedreader,stringbuilder,bufferedwriter,Java,Bufferedreader,Stringbuilder,Bufferedwriter,我有一个程序,可以读取一个文件,我可以更改这个文件的内容,然后将它写入另一个文件。输入文件是这样的:输出必须是这样的,我当前的输出是这样的,那么如何更改我的代码,使其在12个字符后开始一行?我还想删除最后一个 public class readFile { String line; StringBuilder buf = new StringBuilder(); public void readFile(){ Buf
public class readFile {
String line;
StringBuilder buf = new StringBuilder();
public void readFile(){
BufferedReader reader = null;
try {
File file = new File("C:/Users/Sybren/Desktop/Invoertestbestand1.txt");
reader = new BufferedReader(new FileReader(file));
//String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
//buf.append(line);
processInput();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
};
}
}
public void processInput(){
buf.append(line);
if (buf.length()>7){
buf.append("-");
//buf.append(System.getProperty("line.separator"));
}
/* start with a new line if the line length is bigger than 12 - in progress*/
/* I know this if doesn't work but how to fix it? */
if (buf.length()>12){
buf.append(System.getProperty("line.separator"));
}
/* if a * is followed by * change them to a !*/
for (int index = 0; index < buf.length(); index++) {
if (buf.charAt(index) == '*' && buf.charAt(index+1) == '*') {
buf.setCharAt(index, '!');
buf.deleteCharAt(index+1);
//buf.deleteCharAt(buf.length()-1);
}
// get last character from stringbuilder and delete
//buf.deleteCharAt(buf.length()-1);
}
}
public void writeFile() {
try {
String content = buf.toString();
File file = new File("C:/Users/Sybren/Desktop/test.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
公共类读取文件{
弦线;
StringBuilder buf=新的StringBuilder();
公共void readFile(){
BufferedReader reader=null;
试一试{
File File=新文件(“C:/Users/Sybren/Desktop/invotertestbestand1.txt”);
reader=newbufferedreader(newfilereader(file));
//弦线;
而((line=reader.readLine())!=null){
系统输出打印项次(行);
//buf.追加(行);
processInput();
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
reader.close();
}捕获(IOE异常){
e、 printStackTrace();
};
}
}
public void processInput(){
buf.追加(行);
如果(buf.length()>7){
buf.追加(“-”);
//buf.append(System.getProperty(“line.separator”);
}
/*如果行长度大于12,则从新行开始-正在进行*/
/*我知道这不起作用,但如何修复*/
如果(buf.length()>12){
buf.append(System.getProperty(“line.separator”);
}
/*如果*后跟*将其更改为a*/
对于(int index=0;index
}更新代码,在阅读文件时,您将在其中做出决定:
int sevenCount = 0;
int fourteenCount = 0;
int data = 0;
while ((data = reader.read()) != -1) {
sevenCount++;
fourteenCount++;
if(sevenCount==7)
{
buf.append("-"); // append - at every 7th character
sevenCount = 0;
}
if(fourteenCount==14)
{
buf.append("\n"); // change line after evrry 14th character
fourteenCount = 0;
}
if(((char)data) == '*')
{
char c = '!'; //Change the code when char contain *
data = (int)c;
}
else
{
buf.append((char)data);
}
}
如果要每隔12个字符在字符串中插入换行符:
str = str.replaceAll(".{12}", "$0\n");
最后一个什么?12个字符中的最后一个字符?否当前输出中的最后一个感叹号基本上是否希望将文件作为字符串获取,删除换行符,然后在每12个字符后使用换行符输出?我希望读取文件,进行一些更改并将其写入另一个文件。其中一个更改是在每12个字符后开始换行。我想您的意思是“\n”而不是“\\n”和buf.append(System.getProperty(“line.separator”);也可以,但我想知道如何在12个字符后开始一行,这就是problem@Sybren实际上,由于防火墙的原因,我无法访问附加的文件,因此我错过了pblm。请给我一些时间给你正确的答案。@Sybren你能粘贴输入和预期的输出而不是附加的文件吗?当我尝试复制/粘贴输入/输出时在这里或我的问题中,有些字符会自动更改(不要问我为什么)。请看一下提供的链接。不幸的是,它不起作用,只有第一行有正确的长度(我的代码也这样做了),你用一个替换了一个*!但是下面的两个*必须被一个*替换!。另一件事是,我必须使用.readLine()来读取,而不是.read()。这是一种不错的方法,但缺点是需要将整个文件加载到字符串中才能工作。这将为大文件创建问题。除非您的文件很大,否则我将使用StringBuffer来构建新内容,然后通过上述调用传递其
toString()
,然后将其写入新文件。