📄 lastconnections.java
字号:
/* * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *///TODO: Having capacity passed to these methods is redundant, take it out//*** (should it be in GUI dir?)package net.sf.jftp.gui;import net.sf.jftp.JFtp;//***import net.sf.jftp.config.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.lang.*;import java.util.*;import javax.swing.*;public class LastConnections{ public static String SENTINEL = new String("********************"); private static JFtp jftp; //*** changed this so that JFtp object is passed to it and //initialized public LastConnections(JFtp jftp) { this.jftp = jftp; //init(); } //writeToFile: This code is called when modifications are done //being made (should it be private and called from inside //this class?) //maybe I should return a boolean value stating whether or not it //succeeded //SHOULD THIS BE PRIVATE? //public static void writeToFile(String[] a, int capacity) { public static void writeToFile(String[][] a, int capacity) { try { File f1 = new File(Settings.appHomeDir); f1.mkdir(); File f2 = new File(Settings.last_cons); f2.createNewFile(); FileOutputStream fos; PrintStream out; fos = new FileOutputStream(Settings.last_cons); out = new PrintStream(fos); //String[] lastCons = new String[capacity]; //lastCons = readFromFile(capacity); for(int i = 0; i < capacity; i++) { //out.println(a[i]); int j = 0; out.println(a[i][j]); while((j < JFtp.CAPACITY) && !(a[i][j].equals(SENTINEL))) { j++; out.println(a[i][j]); //retVal[i][j] = raf.readLine(); //System.out.println(); } //System.out.println(a[i]); } /* RandomAccessFile raf = new RandomAccessFile(f2, "rw"); for (int i = 0; i < capacity; i++) { raf.writeChars(a[i] + "\n"); } */ jftp.updateMenuBar(); } catch(Exception e) { e.printStackTrace(); } } //writeToFile public static String[][] readFromFile(int capacity) { //MAKE THIS 2D //String[] retVal = new String[capacity]; String[][] retVal = new String[capacity][JFtp.CONNECTION_DATA_LENGTH]; //if ((f.exists())) // System.out.println("not found"); try { File f1 = new File(Settings.appHomeDir); f1.mkdir(); File f2 = new File(Settings.last_cons); //File f; //f = init(capacity); if(!f2.exists()) { init(capacity); } /* FileReader fr = new FileReader(Settings.last_cons); BufferedReader breader = new BufferedReader(fr); for (int i=0; i<capacity; i++) { retVal[i] = breader.readLine(); } */ RandomAccessFile raf = new RandomAccessFile(f2, "r"); //BUGFIX 1.40: determine if old style of file is // being used //THIS MAY MAKE THIS SECTION LESS TIME-EFFICIENT String[] oldValues = new String[capacity]; String firstSection = new String(""); boolean oldVersion = true; //System.out.println(capacity); for(int i = 0; i < capacity; i++) { if(capacity < JFtp.CAPACITY) { oldVersion = false; break; } oldValues[i] = raf.readLine(); firstSection = oldValues[i].substring(0, 3); //System.out.print(i); //System.out.print(firstSection); //System.out.println("end"); if(!(firstSection.equals("FTP")) && !(firstSection.equals("SFT")) && !(firstSection.equals("SMB")) && !(firstSection.equals("NFS")) && !(firstSection.equals("nul"))) { //System.out.println("###"); oldVersion = false; } if(!oldVersion) { //System.out.println("!!!"); break; } } //for raf = new RandomAccessFile(f2, "r"); if(oldVersion) { //System.out.println("old file detected"); for(int i = 0; i < capacity; i++) { oldValues[i] = raf.readLine(); } changeFile(oldValues); } //reset to read start of file raf = new RandomAccessFile(f2, "r"); for(int i = 0; i < capacity; i++) { int j = 0; retVal[i][j] = raf.readLine(); //System.out.print("--->"); //System.out.println(retVal[i][j]); while((j < JFtp.CONNECTION_DATA_LENGTH) && !(retVal[i][j].equals(SENTINEL))) { j++; retVal[i][j] = raf.readLine(); //System.out.print("--->"); //System.out.println(retVal[i][j]); //j++; } //while //retVal[i][j+1] = raf.readLine(); } //for } catch(Exception ex) { ex.printStackTrace(); } return retVal; } //readFromFile public static String[][] prepend(String[] newString, int capacity, boolean newConnection) { //BUGFIX: 2D //String[] lastCons = new String[capacity]; String[][] lastCons = new String[capacity][JFtp.CONNECTION_DATA_LENGTH]; lastCons = readFromFile(capacity); for(int i = 0; i < capacity; i++) { int j = 0; while(!(lastCons[i][j].equals(SENTINEL))) { //temp[j] = lastCons[i][j]; //lastCons[i][j] = newString[j]; //System.out.println(lastCons[i][j]); j++; } //System.out.println(lastCons[i][j]); } //BUGFIX: now need array to represent 1st connection data //String temp = new String(""); String[] temp = new String[JFtp.CONNECTION_DATA_LENGTH]; int j = 0; //is temp necessary? //System.out.println("++++++++++++++++++++"); while(!(lastCons[0][j].equals(SENTINEL))) { //while (!(newString[j].equals(SENTINEL))) { temp[j] = lastCons[0][j]; //lastCons[0][j] = newString[j]; //System.out.println(lastCons[0][j]); //if (lastCons[0][j].equals(SENTINEL)) //break; j++; } temp[j] = SENTINEL; j = 0; while(!(newString[j].equals(SENTINEL))) { lastCons[0][j] = newString[j]; j++; } lastCons[0][j] = SENTINEL; //take out any data that would be "left over" after sentinel //is this necessary? j++; while(j < JFtp.CONNECTION_DATA_LENGTH) { lastCons[0][j] = ""; j++; } //System.out.println("-----------------"); for(int i = 0; i < capacity; i++) { //while (!(lastCons[j].equals(SENTINEL))) { //newString = temp; if((i + 1) != capacity) { //System.out.print("-->"); j = 0; //System.out.println(temp[j]); //shift temp data into newString while(!(temp[j].equals(SENTINEL))) { newString[j] = temp[j]; //System.out.println(newString[j]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -