您的位置:首页 > 编程语言 > Java开发

dos中编译、运行java文件,并把显示结果保存本地txt中

2017-07-04 00:00 483 查看
本文永久地址:https://my.oschina.net/bysu/blog/1154361

---------------------本文最近更新时间:2017年8月9日11:46:31-------------------------

在这篇博客https://my.oschina.net/bysu/blog/994658的前面曾提到通过批处理编译、运行java文件。结果只能在dos窗口中显示,关闭之后就没有了。事后想“翻旧账”都不行。为此,把结果保存本地txt文件中。PS:当然也可以直接通过java代码就直接把结果同时写入文件。

@echo off
:1
javac "D:\bysu\interfaceAuto\MobileRandomNum.java"
cd "D:\bysu\interfaceAuto\"

for /f "delims=" %%i in ('java MobileRandomNum') do (
echo %%i
echo "%%i" -- %date:~0,10% %time:~0,8%>> result.txt

::echo "%%i"  -- %date%>> result.txt
)
pause
goto 1

运行以上批处理,会在D:\bysu\interfaceAuto\路径中创建result.txt文件,并追加内容。

以上相应的java类

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class MobileRandomNum {

public static void main(String[] args) {
String channelName = channelName();
System.out.println(channelName);
System.out.println(startMobileNo() + endMobileNo());
}

public static int startMobileNo(){
int[] mobileStart = {139,138,137,136,135,134,159,158,157,150,151,152,188,130,131,132,156,155,133,153,189,180,177,176};
Random r = new Random();
ArrayList<Integer> mobileList = new ArrayList<>();
for(int i = 0;i<mobileStart.length;i++){
mobileList.add(mobileStart[i]);
}
return mobileList.get(r.nextInt(mobileList.size()));
}

public static String endMobileNo(){
Random r = new Random();
String temp = "";
for(int i=0;i<8;i++){
temp += r.nextInt(10);
}
return temp;
}

public static String channelName(){
//System.out.println("请输入渠道:1.蚂蚁开户;2.常规开户;3.保单开户;4.融资融券预开户...");
Scanner scan = new Scanner(System.in);
String is_result = String.valueOf(scan.nextInt());
switch(is_result){
case "1":
is_result = "蚂蚁开户";
break;
case "2":
is_result = "常规开户";
break;
case "3":
is_result = "保单开户";
break;
case "4":
is_result = "融资融券预开户";
break;
default:
is_result = "其他";
}
return is_result;
}
}


参考资料:http://blog.csdn.net/devfun/article/details/7105671

--------------------------更新:2017年7月28日12:46:38------------------

新增不同渠道的判断,这样哪个号码对应哪个渠道,才不会混乱

java类

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class MobileRandomNum {

public static void main(String[] args) {
String channelName = channelName();
System.out.println(channelName);
System.out.println(startMobileNo() + endMobileNo());
}

public static int startMobileNo(){
int[] mobileStart = {139,138,137,136,135,134,159,158,157,150,151,152,188,130,131,132,156,155,133,153,189,180,177,176};
Random r = new Random();
ArrayList<Integer> mobileList = new ArrayList<>();
for(int i = 0;i<mobileStart.length;i++){
mobileList.add(mobileStart[i]);
}
return mobileList.get(r.nextInt(mobileList.size()));
}

public static String endMobileNo(){
Random r = new Random();
String temp = "";
for(int i=0;i<8;i++){
temp += r.nextInt(10);
}
return temp;
}

public static String channelName(){
//System.out.println("请输入渠道:1.蚂蚁开户;2.常规开户;3.保单开户;4.融资融券预开户...");
Scanner scan = new Scanner(System.in);
String is_result = String.valueOf(scan.nextInt());
switch(is_result){
case "1":
is_result = "蚂蚁开户";
break;
case "2":
is_result = "常规开户";
break;
case "3":
is_result = "保单开户";
break;
case "4":
is_result = "融资融券预开户";
break;
default:
is_result = "其他";
}
return is_result;
}
}

批处理dat

@echo off

:1
echo "请输入渠道:1.蚂蚁开户;2.常规开户;3.保单开户;4.融资融券预开户;5.其他..."
javac -encoding utf8 "D:\bysu\interfaceAuto\MobileRandomNum.java"
cd "D:\bysu\interfaceAuto\"

for /f "delims=" %%i in ('java MobileRandomNum') do (
echo %%i
echo "%%i" -- %date:~0,10% %time:~0,8%>> result.txt
)
pause
goto 1

说明:加入-encoding utf8 进行编译,java代码中含有中文才不至于报如下错误:编码GB18030的不可映射字符System.out.println("璇疯緭鍏ユ笭閬擄細1.铓傝殎寮??鎴凤紱2.甯歌寮??鎴凤紱3.淇濆崟寮??鎴凤紱4.铻嶈祫铻嶅埜棰勫紑鎴??...");

如果编译的时候,提示第一行错误,可按照截图中的方法,使用notepad++对java源码进行编码保存即可。



PS:如果出现已经点击上图中中指定编码,但是却不被选中的情况,可以通过window自带的文本编辑器,打开,然后另存为的时候选中utf8即可。

-------------------------------------------------------------------------------------

如果需要把dos中的命令值传给java,可以参考以下地址;
http://www.iteye.com/problems/99457
Java代码 :

public class BatTest {

public static void main(String[] args) {
if(args.length < 1) {
System.out.println("please input your args");
System.exit(-1);
}
System.out.println("ok,your input is "+args[0]);

}
}

脚本:

bat脚本类似如下,假设名称为start.bat:

@echo off

set ACTION="%1"

rem java -classpath xxx.jar com.test.TestClass %ACTION%

java BatTest %ACTION%

dos:

命令行里这样执行:

start.bat Hello(这是你的传入值)

-------------------------------更新时间:2017年8月9日11:46:31-----------------------------------------

有时候需要看最近的历史记录,每次都需要层层剥开(打开)本地文件目录,经常这样,我这玻璃心会觉得累,于是继续优化一下。可以获取最近10行记录,当然也可以根据时间来判断,但是懒得写了,就酱紫吧!新增一个java类,之前的java类不变。

GetSourceFileLineNo类

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class GetSourceFileLineNo{

public static void main(String[] args) {
getLimitRecord("./result.txt");
}

public static void getLimitRecord(String filePath){
BufferedReader br = null;
String tempStr = "";
int count = 0;
int allLineOfFile = getSourceFileLineNo(filePath);//文本文件的总行数
try{
br = new BufferedReader(new FileReader(new File(filePath)));
while((tempStr=br.readLine())!=null){
if((allLineOfFile - count++)<=10){//最后10行记录
System.out.println(tempStr);
}
}
}catch(IOException e){
System.out.println("没有该文件或目录");
e.printStackTrace();
}finally{
try{
br.close();
}catch(IOException e){
System.out.println("关闭以前关闭的流无效~!");
e.printStackTrace();
}
}
}

//Gets all lines of file,获取文本文件的行数
public static int getSourceFileLineNo(String filePath){
BufferedReader br = null ;
String tempStr = "";
int count = 0;
try{
br = new BufferedReader(new FileReader(new File(filePath)));
while((tempStr=br.readLine())!=null){
count ++;
}
}catch(IOException fnf){
System.out.println("没有该文件或目录");
fnf.printStackTrace();

}finally{
try{
br.close();
}catch(IOException e){
System.out.println("关闭以前关闭的流无效~!");
e.printStackTrace();
}
}
return count;
}
}

当然,批处理文件也要进行相应的更改,如下:

@echo off
echo "最近的历史记录~:"
javac -encoding utf8 "D:\bysu\interfaceAuto\GetSourceFileLineNo.java"
cd "D:\bysu\interfaceAuto\"
for /f "delims=" %%i in ('java GetSourceFileLineNo') do (
echo %%i
)

:1
echo "请输入渠道:1.蚂蚁开户;2.常规开户;3.保单开户;4.融资融券预开户;5.其他..."
javac -encoding utf8 "D:\bysu\interfaceAuto\MobileRandomNum.java"
cd "D:\bysu\interfaceAuto\"

for /f "delims=" %%i in ('java MobileRandomNum') do (
echo %%i
echo "%%i" -- %date:~0,10% %time:~0,8%>> result.txt
)
pause
goto 1

最后运行的效果



本文永久地址:https://my.oschina.net/bysu/blog/1154361
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: