程序文件:
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的值,方便其他类调用,先谢谢~在线等~解决立即结分
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的值,方便其他类调用,先谢谢~在线等~解决立即结分
解决方案 »
- java 获取当前时间不正确
- [求助]什么情况下必须用finally? 请举个简单的例子吧?
- 再论main线程与子线程交互问题. [代码]
- java题目->come in
- JAVA的一些问题
- 问一个关于 null 的问题
- 初学,搞不懂接口,大家来指点我一下如果理解接口
- 刚刚在JDK文档上看见个方法setName(String s)可以设置组件上的字符串可我试拉试怎么不行请进来帮我看看好吗
- 除了sun的主页,那还有快一点的下jdk1.4的快一点的地方?
- java程序中如何为一个while(true)循环记录次数,并跳出循环后显示循环次数
- HELP ,我都急疯了
- finally关闭文件出错啦~!~!帮忙看看~~
public static void main(String[] args) throws Exception{
}
里面的 strFileName就可以了
楼主想要实现什么样的功能?
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;
}}}