cdcsvfile.java

来自「一个日本流行的,功能较全的开源Web办公管理(Groupware)系统。」· Java 代码 · 共 326 行

JAVA
326
字号
package jp.co.sjts.gsession.card;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;

import java.util.TreeSet;
import java.text.Collator;
import jp.co.sjts.gsession.tools.*;

/**
 * <p>Card 偺僄僋僗億乕僩丄僀儞億乕僩梡 File
 *
 * @author Ei KISHIDA &lt;ei@sjts.co.jp&gt;
 * @author Hideyuki KITADE &lt;kitade@sjts.co.jp&gt;
 */
public class CDCsvFile extends GSTextFile{

	/** 僄僋僗億乕僩僼傽僀儖柤 */
	public static String FILENAME1 = CDCmd.EXPFILENAME;
	/**  */
	public static String FILENAME2 = CDCmd.INPFILENAME;

	/** */
	private String    filePath;
	/** */
	private List      IdxList  = null;
	/** */
	private String    path     = null;
	/** */
	private File      file     = null;
	/** */
	private CDCsvData data     = null;
	/** */
	private int       hitcnt;

	/**
	 * <p>弶婜壔傪峴偄傑偡丅
	 *
	 * @param HomeDir
	 * @param id
	 * @param mod
	 */
	public CDCsvFile(String HomeDir,String id,int mod) throws GSException{
		init();
		filePath = HomeDir + CDCmd.CD_IX_TN + file.separator + id ;//ID偼儐乕僓ID
		SetPath(filePath);

		// 僨傿儗僋僩儕偺桳岠惈傪僠僃僢僋
		try{
			File file1 = new File(filePath);

			if(!file1.exists())	   // 僼傽僀儖偑懚嵼偡傞偐
			{
				if(!file1.mkdirs())      // 僼傽僀儖偑懚嵼偟側偄応崌偼丄嶌惉傪帋傒傞
					throw new GSException("僨傿儗僋僩儕嶌惉偵帋傒偨偑幐攕偟傑偟偨丅");
			}

		} catch (SecurityException e) {
			throw new GSException("傾僋僙僗尃僄儔乕:"+filePath);
		}

		if (mod == CDCmd.CD_IMPORT){//僀儞億乕僩
			filePath = filePath + file.separator + FILENAME2;
			this.file = new File(filePath);
			Load();
		}else{//僄僋僗億乕僩
			filePath = filePath + file.separator + FILENAME1;
			this.file = new File(filePath);
			Save();
		}

	}

	/** 
	 * <p>弶婜壔
	 */
	public synchronized void init(){
		IdxList = Collections.synchronizedList(new ArrayList());
	}

	/** 
	 * <p>PATH愝掕
	 *
	 * @param path 僷僗
	 */
	public synchronized void SetPath(String path){
		this.path = path;
	}

	/**
	 * <p>僒僀僘庢摼
	 *
	 * @return 僒僀僘
	 */
	public synchronized int size(){
		return IdxList.size();
	}


	/** 
	 * 捛壛
	 *
	 * @param mod 僨乕僞
	 */
	public synchronized void Addmod(CDCsvData mod){
		synchronized(IdxList){
			IdxList.add(mod);
		}
	}
	

	/**
	 * <p>撉傒崬傒
	 *
	 * @param index
	 * @return CDCsvData
	 */
	public synchronized CDCsvData Getmod(int index){
		CDCsvData mod = null;
		synchronized(IdxList){
			if( (index>=0)&&(index<IdxList.size()))
				mod = (CDCsvData)IdxList.get(index);
			return mod ;
		}
	}

	/**
	 * <p>CSV宍幃偱僨乕僞嶌惉
	 *
	 * @return CVS宍幃偵偟偨僨乕僞攝楍
	 */
	public synchronized String[] toModsString(){
		synchronized(IdxList){
			Iterator it=IdxList.iterator();
			String[] mods=new String[IdxList.size()];
			for(int i=0;i<IdxList.size();i++){
				CDCsvData mod = (CDCsvData)it.next();
				mods[i] = 
					mod.getEmployname() + "," + mod.getConame()   + "," + mod.getCokname() + "," + mod.getBelong() + "," 
					+ mod.getPosition() + "," + mod.getName()     + "," + mod.getKname()   + "," + mod.getPostno() + "," 
					+ mod.getAddress1() + "," + mod.getAddress2() + "," + mod.getCotel()   + "," + mod.getCofax()  + "," 
					+ mod.getPbtel()    + "," + mod.getPtel()     + "," + mod.getMail()    + "," + mod.getUrl()    + "," 
					+ mod.getMemo()     + ",";
			}
			return mods;
		}
	}

	/**
	 * <p>僼傽僀儖僆僽僕僃僋僩傪曉偟傑偡丅
	 *
	 * @return 僼傽僀儖僆僽僕僃僋僩
	 */
	public synchronized File GetFile(){
		return file;
	}

	/**
	 * <p>僼傽僀儖偵儔僀僩偟傑偡丅
	 * <p>偙偺Write儊僜僢僪傪捈愙巊梡偡傞偙偲偼偁傝傑偣傫丅Save儊僜僢僪偐傜屇傃弌偝傟傑偡丅
	 * <BR>僥儞僾儗乕僩儊僜僢僪僷僞乕儞
	 *
	 * @param pw PrintWriter僆僽僕僃僋僩
	 */
	public synchronized void Write(PrintWriter pw) throws GSException{
		String[] buf = toModsString();
		for(int i=0;i<buf.length;i++){
			pw.println(buf[i]);
		}
	}

	/**
	 * <p>僨乕僞傪撉傒崬傒傑偡丅
	 * <p>偙偺Read儊僜僢僪傪捈愙巊梡偡傞偙偲偼偁傝傑偣傫丅Load儊僜僢僪偐傜屇傃弌偝傟傑偡丅
	 * <BR>僥儞僾儗乕僩儊僜僢僪僷僞乕儞
	 *
	 * @param bf 僶僢僼傽乕儕乕僟乕
	 */
	public synchronized void Read(BufferedReader bf) throws GSException{
		String   buf = "";
		GSCsvTokenizer  cdps       ;
		String   sim        ; //侾僨乕僞奿擺梡
		int      intCnt = 0 ; //堦峴栚傪柍帇偡傞偨傔偺僇僂儞僞乕

		try{
			StringBuffer strb = new StringBuffer();

			while (buf != null){
				char[] cbuf = new char[1025];
				int i = bf.read(cbuf,0,1024);
				if (i >0){
					buf = new String(cbuf);
					strb.append(buf);
				}else{
					buf = null;
				}
			}

			GSCsvRecord gscr = new GSCsvRecord(new String(strb));
			while(gscr.hasMoreRecord()){
				buf = gscr.getNextRecord();

				cdps = new GSCsvTokenizer(buf);
				if (intCnt != 0){

					//嬈庬柤,夛幮柤,夛幮柤偐側,強懏
					String employname = "";
					if (cdps.hasMoreTokens()){
						employname = cdps.nextToken();
					}
					
					String coname = "";
					if (cdps.hasMoreTokens()){
						coname = cdps.nextToken();
					}
					
					String cokname = "";
					if (cdps.hasMoreTokens()){
						cokname = cdps.nextToken();
					}
					
					String belong = "";
					if (cdps.hasMoreTokens()){
						belong = cdps.nextToken();
					}

					//,栶怑,柤慜,柤慜偐側,梄曋斣崋,
				  				  
					String	position = "";
					if (cdps.hasMoreTokens()){
						position = cdps.nextToken();
					}
				 
					String	name = "";
					if (cdps.hasMoreTokens()){
						name = cdps.nextToken();
					}
				  
					String	kname = "";
					if (cdps.hasMoreTokens()){
						kname = cdps.nextToken();
					}

					String	postno = "";
					if (cdps.hasMoreTokens()){
						postno = cdps.nextToken();
					}


					//廧強侾,廧強俀,夛幮揹榖,夛幮FAX,
					
					String	address1 = "";
					if (cdps.hasMoreTokens()){
						address1 = cdps.nextToken();
					}
				  				  
					String	address2 = "";
					if (cdps.hasMoreTokens()){
						address2 = cdps.nextToken();
					}
				  
					String	cotel = "";
					if (cdps.hasMoreTokens()){
						cotel = cdps.nextToken();
					}

					String	cofax = "";
					if (cdps.hasMoreTokens()){
						cofax = cdps.nextToken();
					}

					//帺戭揹榖,実懷揹榖,E-MAIL,URL
					String	pbtel = "";
					if (cdps.hasMoreTokens()){
						pbtel = cdps.nextToken();
					}
					
					String	ptel = "";
					if (cdps.hasMoreTokens()){
						ptel = cdps.nextToken();
					}

				  				  
					String	mail = "";
					if (cdps.hasMoreTokens()){
						mail = cdps.nextToken();
					}
				  
					String	url = "";
					if (cdps.hasMoreTokens()){
						url = cdps.nextToken();
					}
					
					//,旛峫
					String	memo = "";
					if (cdps.hasMoreTokens()){
						memo = cdps.nextToken();
					}

					Addmod(new CDCsvData(employname,coname,cokname,belong ,
										 position,name, kname,postno,
										 address1,address2,cotel,cofax ,
										 pbtel,ptel,mail,url,
										 memo));
				}
				intCnt = intCnt + 1;
			}			
		}catch(Exception e){
			throw new GSException("晄惓側僨乕僞偱偡丅");
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?