請嘗試以下解決方案。它逐行讀取段落,然後對每行進行處理,以便從0插入第40個索引處的新行字符或插入最後一個新行字符。另外,不是在每一行上執行操作,它可以在整個段落中完成,方法是首先將其附加到構建器對象中並執行插入操作,但對於包含大量數據的文件可能不好。
由於您尚未提及有關包含多個段落的文件的任何內容,因此下面的解決方案最適合單個段落。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class A2Q2 {
public static final int MAX_CHARS_PER_LINE = 40;
public static void main(String[] args) {
printFile();
}
public static void printFile() {
BufferedReader fileIn = null;
FileReader fileReaderIn = null;
String inputLine;
int lengthOfLine;
int newLineCount;
StringBuilder sb = new StringBuilder();
try {
fileReaderIn = new FileReader("A2Q2in.txt");
fileIn = new BufferedReader(fileReaderIn);
// read paragraph line by line
while ((inputLine = fileIn.readLine()) != null) {
// counter for iteration, set on each iteration for new line from paragraph
int ctr = 0;
// append line to string builder object
sb.append(inputLine);
lengthOfLine = sb.length();
// identifies how many newline characters to be added in the line
newLineCount = lengthOfLine/MAX_CHARS_PER_LINE;
// if newCount > 0 is true, signifies that line needs to be broken to 40 chars
if(newLineCount > 0) {
while(ctr < newLineCount) {
// insert newline character at 40th index from last newline character
sb.insert((MAX_CHARS_PER_LINE+(MAX_CHARS_PER_LINE*ctr)+ctr), "\n");
ctr++;
}
System.out.print(sb.substring(0, (MAX_CHARS_PER_LINE*newLineCount)+ctr));
// delete the printed string and keeping the left over to be appended by next line
sb.delete(0, ((MAX_CHARS_PER_LINE*newLineCount)+ctr));
}
}
// print the remaining string left in the builder object.
System.out.print(sb.toString());
}
catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
finally {
if(fileIn != null) {
try {
fileIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fileReaderIn != null) {
try {
fileReaderIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
對於上述代碼的輸入是:
In ancient manuscripts, another means to divide sentences in into paragraphs was a line break
followed by an initial at the beginning of the next paragraph. An initial is an oversize capital
letter, sometimes outdented beyond the margin of text. This style can be seen, for example, in
the original Old English manuscript of Beowulf. Outdenting is still used in English typography,
though not commonly.[4] Modern English typography usually indicates a new paragraph by indenting
the first line. This style can be seen in the (handwritten) United States Constitution from 1787.
For additional ornamentation, a hedera leaf or other symbol can be added to the inter-paragraph
whitespace, or put in the indentation space.
而基於(由於缺乏要求的)假設的輸出是:
In ancient manuscripts, another means to
divide sentences in into paragraphs was
a line break followed by an initial at
the beginning of the next paragraph. An
initial is an oversize capital letter, s
ometimes outdented beyond the margin of
text. This style can be seen, for exampl
e, in the original Old English manuscrip
t of Beowulf. Outdenting is still used i
n English typography, though not commonl
y.[4] Modern English typography usually
indicates a new paragraph by indenting t
he first line. This style can be seen in
the (handwritten) United States Constit
ution from 1787. For additional ornament
ation, a hedera leaf or other symbol can
be added to the inter-paragraph whitesp
ace, or put in the indentation space.
每行只有40個字符。
你看過[substring](http://www.tutorialspoint.com/java/java_string_substring.htm)嗎? – sam
在這一行中,System.out.print(inputLine +「」);試試這個:System.out.print(inputLine.substring(0,40));看看會發生什麼。 –
它給出了一個錯誤信息,指出字符串索引輸出或範圍:40 –