텍스트 파일을 개체로 변환 한 다음 세 가지 별도의 파일 구문 분석 함수를 사용하여 이러한 값을 다시 sqlite 데이터베이스에 삽입합니다. 그것들은 기본적으로 객체 클래스를 제외하고 모두 동일합니다. 다음과 같이리팩토링 텍스트 파일 파싱을 일괄 처리
과정은 간다 :
- 는
- 이 목표 테이블의 이전 기록을 모두 삭제 진행 계산에 파일의 라인 카운트 HTTP를
- 을 사용하여 파일을 다운로드
- BufferedReader를 사용하여 파일 열기
- 한 번에 2000 줄을 읽고이를 개체로 변환하십시오.
- Insert 2000 recor
나는 일반이 코드는 모든 클래스는 객체를 생성 한 다음를 유지하는 데 사용할 DAL 기능 결정을 위해 사용할 수 있도록하는 방법을 알아낼 수 없습니다 데이터. Java는 제 첫 번째 언어가 아니므로 모든 지침이 훌륭합니다.
public void downloadPendingPoleInspections() {
int count;
String filePath;
Inspections inspections = Inspections.getInstance();
filePath = Environment.getExternalStorageDirectory() + File.separator + "inspections.txt";
try {
downloadFile("http://localhost/api/inspectionservices.aspx?o=retrieve", "pendinginspections.txt", POST_PENDING_INSPECTIONS_PROGRESS_UPDATE);
int totalInspections = getLineCount(filePath);
inspections.deleteAllPendingInspections();
File file = new File(filePath);
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int i = 0;
int j = 0;
List<PendingInspection> batch = new ArrayList<PendingInspection>();
while ((line = br.readLine()) != null) {
String[] values = line.split(" ");
PendingInspection pending = new PendingInspection(
Integer.parseInt(values[0]), values[1],
Double.parseDouble(values[2]),
Double.parseDouble(values[3]));
batch.add(pending);
i++;
j++;
if (i >= 2000) {
inspections.pendingInspectionsBatchInsert(batch);
batch.clear();
i = 0;
}
}
if (i > 0) {
inspections.pendingInspectionsBatchInsert(batch);
batch.clear();
}
br.close();
file.delete();
} catch (Exception e) {
Log.e("SyncActivity", e.toString());
}
}
편집 : 여기
내가 사용하고있는 코드는 다음 인터페이스와 클래스 선언은public interface Inspectable {
public int getId();
public void setId(int id);
public String getLabel();
public void setLabel(String label);
public double getX();
public void setX(double x);
public double getY();
public void setY(double y);
}
public class RWInspection {
private String id;
private double x;
private double y;
private String inspector;
private String comments;
private String timestamp;
public RWInspection(String id, double x, double y, String inspector, String comments, String timestamp) {
this.id = id;
this.x = x;
this.y = y;
this.inspector = inspector;
this.comments = comments;
this.timestamp = timestamp;
}
싹둑 .... getter 및 setter 구현입니다
public class PInspection implements Inspectable{
private int id;
private String number;
private double x;
private double y;
public PInspection(int id, String poleNumber, double x, double y) {
this.id = id;
this.number = number ;
this.x = x;
this.y = y;
}
그러면 다음과 같이 간단하게 호출 할 수 있습니다. InspectionDownloader d = new InspectionDownloader(); 그리고 기본 클래스 downloadPendingPoleInspections 메소드는 실제 작업을 처리 할 것인가? – eptiliom
나는이 대답으로 갈 것이다, 고마워. – eptiliom