import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class TextReader {
private List textList;
private String text;
private String viewString,viewNameString;
private int stopper;
public TextReader(){
textList = new LinkedList();
text = "";
viewString = "";
viewNameString = "";
stopper = 0;
}
public void output(){
Iterator it = textList.iterator();
while (it.hasNext()) {
String aStr = (String)it.next();
System.out.println(aStr);
//viewへの出力文字列
viewString += aStr;
stopper++;
if(stopper == 30){//上限行数
break;
}
}
}
public void textEdit(String src){
text = src.replaceAll("
", "\n");
String text_ = "nullpo";
int newLine = text.indexOf("\n");
System.out.println("newLine = " + newLine);
if(text.length() > 30 && newLine < 31){
//規定文字数以上だが改行文字が含まれていた場合の処理;
//改行しても30文字を超えた場合の処理はまだなのでなんとかするように;
int a = text.indexOf("\n");
text_ = text.substring(a);
text = text.substring(0,a);
textList.add(text);
textList.add(text_);
}else if(text.length() > 30){
//文字数が一定を超えたら改行する。60文字以上はまだ処理できないのでなんとかするように;
text_ = text.substring(30);
text = text.substring(0,30) + "\n";
textList.add(text);
textList.add(text_);
}else{
textList.add(text);
}
}
public void stringEditter(String src){
String lookString = "";
int nameSetter = src.indexOf(lookString);
int nameEnd = src.indexOf("");
if(nameSetter > -1){
viewNameString = src.substring((nameSetter + lookString.length()),nameEnd);
}else{
textEdit(src);
}
}
/*public void setText(String t){
text += t;
}*/
public String getNameText(){
return viewNameString;
}
public String getText(){
return viewString;
}
public void reader(String fileName) {
try{
File file = new File(fileName);
if (checkBeforeReadfile(file)){
BufferedReader br = new BufferedReader(new FileReader(file));
String str;
while((str = br.readLine()) != null){
stringEditter(str);
}
br.close();
output();
}else{
System.out.println("ファイルが見つからないか開けません");
}
}catch(FileNotFoundException e){
System.out.println(e);
}catch(IOException e){
System.out.println(e);
}
}
private static boolean checkBeforeReadfile(File file){
if (file.exists()){
if (file.isFile() && file.canRead()){
return true;
}
}
return false;
}
}