BufferedWriter 添加了太多行?
BufferedWriter adds too many lines?
我想问一下是否有比使用 StringBuilder 切割 String 的一部分更好的解决方案,String 是通过 BufferedWriter 从方法解析到文件的?
如果没有 StringBuilder,文件中会有一堆字符串,其余的字符串会发生什么情况,data-nirvana?
class TaskStartPart {
public void calculHash() throws InterruptedException, IOException, NoSuchAlgorithmException {
try {
DigestInputStream digestInputStream=null ;
MessageDigest messageDigest=MessageDigest.getInstance("SHA-512") ;
digestInputStream=new DigestInputStream(new TaskPart(new File("C:\Users\win7p\Documents/t")),messageDigest) ;
while(digestInputStream.read()>=0) ;
for(byte b: messageDigest.digest()) {
hexRes2 += String.format("%02x",b);
} sb = new StringBuilder(hexRes2); //StringBuilder which adjust the string to be parsed
dateiSpeichern(0,0,sb.substring(hexRes2.length() - 128,hexRes2.length())); System.out.println(sb.substring(hexRes2.length() - 128,hexRes2.length()));
digestInputStream.close();
} catch (IOException ex ) {ex.printStackTrace();}
}
}
class TaskPart extends InputStream {
private File mFile ;
private List<File> mFiles ;
private InputStream mInputStream ;
public TaskPart(File file) throws FileNotFoundException {
mFile=file ;
if(file.isDirectory()) {
mFiles=new ArrayList<File>(Arrays.asList(file.listFiles())) ;
Collections.sort(mFiles) ;
mInputStream=nextInputStream() ;
} else {
mFiles=new ArrayList<File>() ;
mInputStream=new FileInputStream(file) ;
}
}
@Override
public int read() throws IOException {
int result=mInputStream==null?-1:mInputStream.read() ;
if(result<0 && (mInputStream=nextInputStream())!=null)
return read() ;
else return result ;
}
protected String getRelativePath(File file) {
return file.getAbsolutePath().substring(mFile.getAbsolutePath().length()) ;
}
protected InputStream nextInputStream() throws FileNotFoundException {
if(!mFiles.isEmpty()) {
File nextFile=mFiles.remove(0) ;
return new SequenceInputStream(
new ByteArrayInputStream(getRelativePath(nextFile).getBytes()),
new TaskPart(nextFile)) ;
}
else return null ;
}
}
private void dateiSpeichern(int i1, int i2, String hexR) throws InterruptedException, IOException {
try {
String tF = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss").format(new Date().getTime());
try (BufferedWriter writer = new BufferedWriter(new FileWriter("C:\Users\win7p\Documents/hashLog.txt", true))) {
writer.append(tF);
writer.newLine();
writer.append(dtf);
writer.newLine();
writer.append("Hash Value: ");
writer.append(hexR); //without StringBuilder I would have much //strings added next to eachone line by line
} //normal here is also a catch code.
你并不真的需要一个StringBuilder
,因为你只需要用一个String
加载它,然后你只需要取出一个substring 来自它.
你可以直接使用String#substring方法。
在这两种情况下,垃圾回收都会处理无法访问的对象。
StringBuilder
用于 构建 一个 String
,本质上是一个 可变字符缓冲区 ,你当你想多次改变一个字符串时使用它(这不是你的情况)。
我想问一下是否有比使用 StringBuilder 切割 String 的一部分更好的解决方案,String 是通过 BufferedWriter 从方法解析到文件的?
如果没有 StringBuilder,文件中会有一堆字符串,其余的字符串会发生什么情况,data-nirvana?
class TaskStartPart {
public void calculHash() throws InterruptedException, IOException, NoSuchAlgorithmException {
try {
DigestInputStream digestInputStream=null ;
MessageDigest messageDigest=MessageDigest.getInstance("SHA-512") ;
digestInputStream=new DigestInputStream(new TaskPart(new File("C:\Users\win7p\Documents/t")),messageDigest) ;
while(digestInputStream.read()>=0) ;
for(byte b: messageDigest.digest()) {
hexRes2 += String.format("%02x",b);
} sb = new StringBuilder(hexRes2); //StringBuilder which adjust the string to be parsed
dateiSpeichern(0,0,sb.substring(hexRes2.length() - 128,hexRes2.length())); System.out.println(sb.substring(hexRes2.length() - 128,hexRes2.length()));
digestInputStream.close();
} catch (IOException ex ) {ex.printStackTrace();}
}
}
class TaskPart extends InputStream {
private File mFile ;
private List<File> mFiles ;
private InputStream mInputStream ;
public TaskPart(File file) throws FileNotFoundException {
mFile=file ;
if(file.isDirectory()) {
mFiles=new ArrayList<File>(Arrays.asList(file.listFiles())) ;
Collections.sort(mFiles) ;
mInputStream=nextInputStream() ;
} else {
mFiles=new ArrayList<File>() ;
mInputStream=new FileInputStream(file) ;
}
}
@Override
public int read() throws IOException {
int result=mInputStream==null?-1:mInputStream.read() ;
if(result<0 && (mInputStream=nextInputStream())!=null)
return read() ;
else return result ;
}
protected String getRelativePath(File file) {
return file.getAbsolutePath().substring(mFile.getAbsolutePath().length()) ;
}
protected InputStream nextInputStream() throws FileNotFoundException {
if(!mFiles.isEmpty()) {
File nextFile=mFiles.remove(0) ;
return new SequenceInputStream(
new ByteArrayInputStream(getRelativePath(nextFile).getBytes()),
new TaskPart(nextFile)) ;
}
else return null ;
}
}
private void dateiSpeichern(int i1, int i2, String hexR) throws InterruptedException, IOException {
try {
String tF = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss").format(new Date().getTime());
try (BufferedWriter writer = new BufferedWriter(new FileWriter("C:\Users\win7p\Documents/hashLog.txt", true))) {
writer.append(tF);
writer.newLine();
writer.append(dtf);
writer.newLine();
writer.append("Hash Value: ");
writer.append(hexR); //without StringBuilder I would have much //strings added next to eachone line by line
} //normal here is also a catch code.
你并不真的需要一个StringBuilder
,因为你只需要用一个String
加载它,然后你只需要取出一个substring 来自它.
你可以直接使用String#substring方法。
在这两种情况下,垃圾回收都会处理无法访问的对象。
StringBuilder
用于 构建 一个 String
,本质上是一个 可变字符缓冲区 ,你当你想多次改变一个字符串时使用它(这不是你的情况)。