이것은 기본적으로 내가하려는 일입니다.파일 -> 바이트 [] -> 문자열 -> 바이트 [] -> 파일 변환
나는 문자열을 검색 MySQL의 테이블
의 String
스토어 그것으로 바이트 배열로 파일을
의 전원을 켭니다
전원을 켜고 그것을
전원을 켜고 그것을 가지고 싶어 그것을 다시 바이트 배열로 변환
T 다시 파일로 돌려 보내십시오.
이제, 제가 할 수있는 한 최선을 다하려고 노력한 코드가 있습니다. 내 문제는이 코드의 마지막 부분에있는 파일이 제대로 나오지 않는다는 것입니다. 정보가 누락되었습니다. 이 파일은 텍스트 파일이므로 파일이 완전한지 아닌지를 알 수 있어야합니다.
필자가 볼 수있는 한, 전체 파일이 아니라 파일의 마지막 부분 만 얻는 것 같습니다. 나는이 전환의 어떤 곳을 나쁘게 어수선하게 만들었다. 이 변환 및 검색을보다 효율적으로 수행하는 방법에 대한 제안이있는 경우 (여전히 데이터베이스와 모든 것을 염두에 두십시오) 알려 주시기 바랍니다!
코드는
import java.io.*;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class main {
public static void main(String[] args) {
// The file we want to save.
File f = new File("build.xml");
try {
// Make it into a byte array first
FileInputStream fis = new FileInputStream(f);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for(int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
System.out.println("read " + readNum + " bytes,");
}
StringBuilder s = new StringBuilder();
// Now we simulate making it into a String, for easier storage
// in a database.
for(byte b : buf) {
// for debugging
s.append(b).append(",");
System.out.print(b +",");
}
// Now we want to retrieve the file from the database as a string
File someFile = new File("build2.xml");
FileOutputStream fos = new FileOutputStream(someFile);
// We count how many bytes there are in this string.
// One byte per Token.
StringTokenizer st = new StringTokenizer(s.toString(),",");
buf = new byte[st.countTokens()];
int i = 0;
StringBuilder t = new StringBuilder();
// Now we parse out all Bytes from the string, and put them into
// the prepared byte array.
while(st.hasMoreTokens()) {
byte b = Byte.parseByte(st.nextToken());
System.out.print(b + ",");
buf[i] = b;
i++;
// for debugging
t.append(b).append(",");
}
// Here I print true if both strings are exactly the same
// which they should be, which means that the bytes are intact
// before and after conversion.
System.out.println("\n" +(t.toString().equals(s.toString()) ? true : false));
// Here we would make the physical file on the machine.
fos.write(buf);
fos.flush();
fos.close();
} catch (IOException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
당신은 단순히 문자열 생성을 엉망
향후 귀하의 질문에 직접 코드를 게시하십시오. –
나는 항상 틀린 것을 얻고, 그 후에 체재는 무섭게 본다. – OmniOwl
코드 샘플을 사용하여 텍스트와 구별합니다. – Venki