这是我最后拼凑起来的一门课。我想将文本文件分成块,然后使用行号通过 Servlet 发送块以获取相关数据。这是一个在我家的服务器上运行的应用程序,它允许我跨不同设备读取我的文本文件,并保留与文件相关的元数据。
package net.stevenpeterson.bookreaderlib;
public class SplitStringUtility {
private String subject;
public SplitStringUtility(String subject) {
this.subject = subject;
}
public String getRow(int rowAddress, int charsPerRow) {
String result = "";
if (rowAddress >= 0 && charsPerRow > 0) {
int startOfSubString = rowAddress * charsPerRow;
int endOfSubString = startOfSubString + charsPerRow;
result = getSubString(startOfSubString, endOfSubString);
}
return result;
}
private String getSubString(int startOfSubString, int endOfSubString) {
String result = "";
if (startOfSubString <= subject.length()) {
if (endOfSubString <= subject.length()) {
result = subject.substring(startOfSubString, endOfSubString);
} else {
result = subject.substring(startOfSubString);
}
}
return result;
}
}
我测试了:
package net.stevenpeterson.bookreaderlib;
import static org.junit.Assert.*;
import net.stevenpeterson.bookreaderlib.SplitStringUtility;
import org.junit.Test;
public class SplitStringUtilityTest {
public final String empty = "";
public final String single ="a";
public final String twoChars = "ab";
public final String threeChars = "abc";
public final String manyChars = "abcdefghijklmnopqrstuvwxyz";
private SplitStringUtility splitter;
@Test
public void splitTestsEmpty() {
makeSplitter(empty);
assertEquals("We are trying to get a non-existant string",
"",
splitter.getRow(3,4));
}
@Test
public void splitTestsNegativeRowOrColumn() {
makeSplitter(manyChars);
assertEquals("We are trying to get a non-existant string",
"",
splitter.getRow(-3,4));
assertEquals("We are trying to get a non-existant string",
"",
splitter.getRow(-1,-1));
assertEquals("We are trying to get a non-existant string",
"",
splitter.getRow(1,-1));
}
@Test
public void splitTestsSingleCharacterStrings() {
makeSplitter(single);
assertEquals("split the string consisting of 1 character",
"a",
splitter.getRow(0,1));
assertEquals("split the string consisting of 1 character, " +
"but ask for two characters, " +
"the string containing only a single char " +
"should be returned","a", splitter.getRow(0,2));
}
@Test
public void splitTestsTwoChars() {
makeSplitter(twoChars);
assertEquals("Row #0 of the ab string in 1 column rows",
"a",
splitter.getRow(0,1));
assertEquals("Row #1 of the ab string in 1 column rows",
"b",
splitter.getRow(1,1));
assertEquals("Row #0 of the ab string in 2 column rows",
"ab",
splitter.getRow(0,2));
assertEquals("Row #1 of the ab string in 4 column rows "
+"should return a blank string",
"",
splitter.getRow(1,4));
assertEquals("Row #0 of the ab string in 4 column rows "
+"should return the ab string",
twoChars,
splitter.getRow(0,4));
}
@Test
public void splitTestsManyChars() {
//split the alphabet into rows of 4 characters, return row 3
//Expect "mnop"
makeSplitter(manyChars);
assertEquals("Row #3 of the alphabet in 4 column rows",
"mnop",
splitter.getRow(3,4));
assertEquals("Row #0 of the alphabet in 4 column rows",
"abcd",
splitter.getRow(0,4));
assertEquals("Row #0 of the alphabet in 26 column rows",
manyChars,
splitter.getRow(0,26));
assertEquals("Row #0 of the alphabet in 26 column rows",
"z",
splitter.getRow(1,25));
assertEquals("Row #0 of the alphabet in 27 column rows"
+ " since the alphabet has 26 characters "
+ "it would be nice to get what we have", manyChars,
splitter.getRow(0,27));
}
private void makeSplitter(String subject){
splitter = new SplitStringUtility(subject);
}
}