程序文件:
public class OrderNum {  static String getCurrentDate() {
java.util.Calendar now = java.util.Calendar.getInstance();
java.text.SimpleDateFormat yearAndMonth =new java.text.SimpleDateFormat("yyyy-MM-dd");
String retString = yearAndMonth.format(now.getTime());
return retString;
}
private static Object lock = new Object();  public OrderNum() {  }
  public static void main(String[] args) throws Exception{
  String logFile = "c:\\lastFile.txt";
  File lastFile = new File(logFile);
   String strFileName ;
   strFileName = "";
  BufferedWriter file = null;
  BufferedReader input = null;
  boolean firstUsed = false;
  try {
  synchronized (lock)
  {
  if (!lastFile.exists()) {
  lastFile.createNewFile();
  firstUsed = true;
  }
  if (firstUsed){
  try{
  file = new BufferedWriter(new FileWriter(logFile));
  file.write(getCurrentDate() + "00000");
  } catch (Exception e){
  System.out.println(e);
  } finally{
  file.close();
  }
  }
  firstUsed = false;
  input = new BufferedReader(new FileReader(logFile));
  String strNo=input.readLine();
  strNo = strNo.substring(strNo.length()-5);
  strNo="00000"+String.valueOf(Integer.parseInt(strNo)+1);
  strNo=strNo.substring(strNo.length()-5);
//---------得到结果  strFileName = getCurrentDate()+strNo;
  System.out.println(strFileName);
  file = new BufferedWriter(new FileWriter(logFile));
  file.write(strFileName);
  }
  } catch (Exception e) {
  System.out.println(e);
  }finally{
  file.close();
  input.close();  }  }
  
public class OrderNum {  static String getCurrentDate() {
java.util.Calendar now = java.util.Calendar.getInstance();
java.text.SimpleDateFormat yearAndMonth =new java.text.SimpleDateFormat("yyyy-MM-dd");
String retString = yearAndMonth.format(now.getTime());
return retString;
}
private static Object lock = new Object();  public OrderNum() {  }
  public static void main(String[] args) throws Exception{
  String logFile = "c:\\lastFile.txt";
  File lastFile = new File(logFile);
   String strFileName ;
   strFileName = "";
  BufferedWriter file = null;
  BufferedReader input = null;
  boolean firstUsed = false;
  try {
  synchronized (lock)
  {
  if (!lastFile.exists()) {
  lastFile.createNewFile();
  firstUsed = true;
  }
  if (firstUsed){
  try{
  file = new BufferedWriter(new FileWriter(logFile));
  file.write(getCurrentDate() + "00000");
  } catch (Exception e){
  System.out.println(e);
  } finally{
  file.close();
  }
  }
  firstUsed = false;
  input = new BufferedReader(new FileReader(logFile));
  String strNo=input.readLine();
  strNo = strNo.substring(strNo.length()-5);
  strNo="00000"+String.valueOf(Integer.parseInt(strNo)+1);
  strNo=strNo.substring(strNo.length()-5);
//---------得到结果  strFileName = getCurrentDate()+strNo;
  System.out.println(strFileName);
  file = new BufferedWriter(new FileWriter(logFile));
  file.write(strFileName);
  }
  } catch (Exception e) {
  System.out.println(e);
  }finally{
  file.close();
  input.close();  }  }
  
public void kk()
  {
    
  }}}
我希望能在 void kk里面返回strFileName的值,方便其他类调用,先谢谢~在线等~解决立即结分

解决方案 »

  1.   

    程序不是那么长的,只要关注
    public static void main(String[] args) throws Exception{
    }
    里面的 strFileName就可以了
      

  2.   

    呵呵:)
    楼主想要实现什么样的功能?
    main方法是应用程序的入口
    你通过建立OrderNum order = new OrderNum()对象的方法是无法执行该方法的如果你仅仅只是要取到strFileName
    你可以采用以下方法:
    public class OrderNum {  ...
      private static Object lock = new Object();
      private String fileName = "";  public OrderNum() {
        readFile();
      }
      private void readFile(){
      String logFile = "c:\\lastFile.txt";
      File lastFile = new File(logFile);
       String strFileName ;
       strFileName = "";
      BufferedWriter file = null;
      BufferedReader input = null;
      boolean firstUsed = false;
      try {
      synchronized (lock)
      {
      if (!lastFile.exists()) {
      lastFile.createNewFile();
      firstUsed = true;
      }
      if (firstUsed){
      try{
      file = new BufferedWriter(new FileWriter(logFile));
      file.write(getCurrentDate() + "00000");
      } catch (Exception e){
      System.out.println(e);
      } finally{
      file.close();
      }
      }
      firstUsed = false;
      input = new BufferedReader(new FileReader(logFile));
      String strNo=input.readLine();
      strNo = strNo.substring(strNo.length()-5);
      strNo="00000"+String.valueOf(Integer.parseInt(strNo)+1);
      strNo=strNo.substring(strNo.length()-5);
    //---------得到结果  strFileName = getCurrentDate()+strNo;
      System.out.println(strFileName);
      file = new BufferedWriter(new FileWriter(logFile));
      file.write(strFileName);
      }
      } catch (Exception e) {
      System.out.println(e);
      }finally{
      file.close();
      input.close();  }  fileName = strFileName;  }
      
      public void kk(){
        // 可以使用fileName
      }  public String getFileName(){
       return this.fileName;
      }}}