자바 ftp 에서 TXT 파일 읽 기 사례

6992 단어 자바ftpTXT
최근 자바 읽 기 ftp 에서 TXT 파일 을 개발 하고 있 습 니 다.그 중 일 부 는 밟 아서 다시 기록 합 니 다.
1.파일 을 읽 을 때 저 는 파일 이름 에 따라 데이터베이스 시트 를 생 성 합 니 다.Oacle 데이터 베 이 스 는 표 이름 의 길이 에 제한 이 있 고 최대 30 글자 입 니 다.
2.여러 파일 의 ftp 읽 기,매번 파일 을 가 져 온 후 다시 돌아 가 는 파일 의 흐름 이 비어 있 습 니 다.즉,순환 중 ftp 에 따라 파일 을 가 져 오 는 흐름 입 니 다.

이러한 상황 이 발생 했 을 때 순환 할 때 ftp 링크 를 열 고 닫 으 면 해결 할 수 있 습 니 다.그렇지 않 으 면 두 번 째 가 져 올 때 input steam 은 null 입 니 다.
3.txt 파일 을 읽 을 때 파일 에 중국어 가 포함 되 어 있 으 면 읽 을 때 오류 가 발생 할 수 있 습 니 다.이것 은 읽 을 수 있 는 문자 집합 을 UTF-8 로 설정 하고 안 되면 다시 시도 합 니 다.
GB2312
4.자바 TXT 파일 읽 기:

InputStreamReader reader = new InputStreamReader(is, "GB2312");
BufferedReader br = new BufferedReader(reader);
String lineTxt = null; //    
int rowNum = 0;
while ((lineTxt = br.readLine()) != null) {}
추가 지식:자바 가 FTP 에서 파일 을 가 져 와 로 컬 로 다운로드 하고 파일 의 내용 을 읽 는 성공 적 인 방법
긴 말 안 할 게 요.그냥 코드 보 세 요~

package com.aof.web.servlet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.jws.WebService;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
@WebService(endpointInterface="com.aof.web.servlet.QualityComplainServices")
public class QualityComplainServicesImpl implements QualityComplainServices {
 //ftp  
  private FTPClient ftp;
  //      ftp  ip
  private String ip = "10.46.249.7";
  //    ,  21
  private int port = 21;
  //     ftp    
  private String name = "DKEDI";
  //     ftp       
  private String pwd = "P@ssw0rd";  
  
 //     ,     ip,  ,     ftp    ,     ftp       。   ftp  ,       fto
  public boolean ftp1() {
    ftp = new FTPClient();
    try {
//     ftp.connect(ip, port);
     if(!ftp.isConnected()){
     ftp.connect(ip, port);
   }
      System.out.println(ftp.login(name, pwd));
//     ftp.setCharset(Charset.forName("UTF-8"));
      ftp.setControlEncoding("UTF-8");
      return true;
    } catch (IOException e) {
      e.printStackTrace();
      return true;
    }
    
  }
  
  public void disconnect() throws Exception {
 if (ftp.isConnected()) {
  ftp.disconnect();
 }
 }
  
 //        
 public boolean download(FTPFile file) throws Exception {
  boolean result = true;
  //       
  File f = new File("E:\\crmFiles\\");
  if (!f.exists()) {
  f.getParentFile().mkdirs();
  }
  long lRemoteSize = file.getSize();
  try {//          
  OutputStream out = new FileOutputStream(f);
  if (f.length() >= lRemoteSize) {
   System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~      ,    ");
   out.flush();
   out.close();
  }
  boolean iss = ftp.retrieveFile(file.getName(), out);
  System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~    \r
"); out.close(); } catch (Exception ex) { ex.printStackTrace(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~ \r
"); return false; } return result; } private InputStreamReader read; private BufferedReader reader; private String preRead(String filepath) throws Exception { File file = new File(filepath); String ordertype = null; if (file.isFile() && file.exists()) { try { read = new InputStreamReader(new FileInputStream(file), "GBK"); reader = new BufferedReader(read); StringBuffer FileContent = new StringBuffer(); String temp = null; while ((temp = reader.readLine()) != null) { FileContent.append(temp); } System.out.println(" ------------------>>>>> "+FileContent+" <<<<<------------------"); } catch (FileNotFoundException e) { System.out.println("!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!"); e.printStackTrace(); } finally { reader.close(); read.close(); // file.delete(); } } return ordertype; } public void gmRead(String remote) throws Exception { boolean downloadResult = false; try { ftp.changeWorkingDirectory(remote); System.out.println(" *************************"+remote); FTPFile[] files = ftp.listFiles(remote); // System.out.println(" *************************"+files.length); for (int i = 0; i < files.length; i++) { FTPFile file = files[i]; if (file.isFile()) { downloadResult = this.download(file);// if (downloadResult) { String ordertype = this.preRead("E:\\crmFiles\\"); } /*// , InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8"); BufferedReader br = new BufferedReader(isr); String lineTxt = null; while ((lineTxt = br.readLine()) != null) { lineTxt+=lineTxt; } System.out.println(lineTxt); br.close();*/ }else{ System.out.println("************* ************"); } } } catch (Exception e) { e.printStackTrace(); } } @Override public String threeDAndEightDReports(String orderNum, String FTPUrl, String FileType) { // 、FTP 、3D/8D System.out.println("1-------------"+orderNum); System.out.println("2-------------"+FTPUrl); System.out.println("3-------------"+FileType); if(null != orderNum && null != FTPUrl && null != FileType){ // FTP boolean flag = this.ftp1(); if(flag){ try { // 、 , this.gmRead(FTPUrl); // this.disconnect(); } catch (Exception e) { e.printStackTrace(); } }else{ System.out.println("!!!!!!!!!!!!!!!!!FTP !!!!!!!!!!!!!!!!!"); } return "success"; }else{ return "fail"; } } public static void main(String[] args) { QualityComplainServicesImpl q = new QualityComplainServicesImpl(); q.threeDAndEightDReports("001","/CRMINTERFACE","3D"); } }
이상 의 자바 가 ftp 에서 TXT 파일 을 읽 은 사례 는 바로 편집장 이 여러분 에 게 공유 한 모든 내용 입 니 다.여러분 에 게 참고 가 되 고 여러분 들 이 저 희 를 많이 사랑 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기