IOフロー学習(2)
34072 ワード
ファイルの読み込みと書き込み
再帰
5階層を求めます
ウサギ匹
ファイルを再帰的に削除
テキストファイルのコピー
画像をコピー
ArrayListコレクションの文字列データをテキストファイルに格納する
テキストファイルからデータ(動作ごとに文字列データ)をコレクションに読み込み、コレクションを巡回します.
私はテキストファイルにいくつかの名前を保存しています.ランダムに一人の名前を取得するプログラムを書いてください.
単極フォルダのコピー
指定したディレクトリの下にある指定ファイルをコピーし、接尾辞名を変更します.
需要:多極フォルダのコピー
キーボードは5人の学生の情報(名前、国語の成績、数学の成績、英語の成績)を入力して、総点によって高いから低いまでテキストのファイルに保存します
学生クラス
既知のs.txtファイルには、「hcexfgijkamdnoqrzstuvwybpl」という文字列があります.
BufferedReaderのreadLine()機能をReaderでシミュレート
MyBufferedReaderをテストするときは、BufferedReaderのように使用します.
BufferedReader
適用
再帰
package cn.itcast_01;
/*
* :
*
* , 。
* Math.max(Math.max(a,b),c);
*
* public void show(int n) {
* if(n <= 0) {
* System.exit(0);
* }
* System.out.println(n);
* show(--n);
* }
*
* :
* A: ,
* B: ,
* C:
*
* :
* A: , , , , :
* , , , , :
* , , , , :
* , , , , :
* ...
* ,
* B: -- -- -- -- -- --
* -- -- -- -- -- --
* -- -- -- -- -- --
* -- -- -- -- -- --
* ...
*
*/
public class DiGuiDemo {
// public DiGuiDemo() {
// DiGuiDemo();
// }
}
5階層を求めます
package cn.itcast_02;
/*
* : 5 。
* :
* 5! = 1*2*3*4*5
* 5! = 5*4!
*
* ?
* A:
* B:
* a:
* b:
* c:
*/
public class DiGuiDemo {
public static void main(String[] args) {
int jc = 1;
for (int x = 2; x <= 5; x++) {
jc *= x;
}
System.out.println("5 :" + jc);
System.out.println("5 :"+jieCheng(5));
}
/*
* :
* :int
* :int n
* :
* if(n == 1) {return 1;}
* :
* if(n != 1) {return n* (n-1);}
*/
public static int jieCheng(int n){
if(n==1){
return 1;
}else {
return n*jieCheng(n-1);
}
}
}
ウサギ匹
package cn.itcast_02;
/*
* , 3 , , , ?
* :
*
* : 1
* : 1
* : 2
* : 3
* : 5
* : 8
* ...
*
* :
* 1,1,2,3,5,8...
* :
* A: ,
* B:
*
* ?
* A:
* B:
* C:
*
* a,b
* :a=1,b=1
* :a=1,b=2
* :a=2,b=3
* :a=3,b=5
* : a b, a+b
*/
public class DiGuiDemo2 {
public static void main(String[] args) {
//
int[] arr = new int[20];
arr[0] = 1;
arr[1] = 1;
// arr[2] = arr[0] + arr[1];
// arr[3] = arr[1] + arr[2];
// ...
for (int x = 2; x < arr.length; x++) {
arr[x] = arr[x - 2] + arr[x - 1];
}
System.out.println(arr[19]);// 6765
System.out.println("----------------");
int a = 1;
int b = 1;
for (int x = 0; x < 18; x++) {
// a
int temp = a;
a = b;
b = temp + b;
}
System.out.println(b);
System.out.println("----------------");
System.out.println(fib(20));
}
/*
* : :int :int n : 1, 1 : ,
*/
public static int fib(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
}
ファイルを再帰的に削除
package cn.itcast_03;
import java.io.File;
/*
* :
*
* :demo
*
* :
* A:
* B: File
* C: File , File
* D: File
* : B
* :
*/
public class FileDeleteDemo {
public static void main(String[] args) {
//
File srcFolder = new File("demo");
//
deleteFolder(srcFolder);
}
private static void deleteFolder(File srcFolder) {
// File
File[] fileArray = srcFolder.listFiles();
if (fileArray != null) {
// File , File
for (File file : fileArray) {
// File
if (file.isDirectory()) {
deleteFolder(file);
} else {
System.out.println(file.getName() + "---" + file.delete());
}
}
System.out
.println(srcFolder.getName() + "---" + srcFolder.delete());
}
}
}
package cn.itcast_03;
import java.io.File;
/*
* : E:\JavaSE java 。
*
* :
* A:
* B: File
* C: File , File
* D: File
* : B
* : .java
* :
* :
*/
public class FilePathDemo {
public static void main(String[] args) {
//
File srcFolder = new File("E:\\JavaSE");
//
getAllJavaFilePaths(srcFolder);
}
private static void getAllJavaFilePaths(File srcFolder) {
// File
File[] fileArray = srcFolder.listFiles();
// File , File
for (File file : fileArray) {
// File
if (file.isDirectory()) {
getAllJavaFilePaths(file);
} else {
// .java
if (file.getName().endsWith(".java")) {
//
System.out.println(file.getAbsolutePath());
}
}
}
}
}
テキストファイルのコピー
package cn.itcast_01;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
*
*
* :
* , , , 。
* , 。
* 5 , 5 。 5 。
* :
* c:\\a.txt -- FileReader -- BufferdReader
* :
* d:\\b.txt -- FileWriter -- BufferedWriter
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
String srcString = "c:\\a.txt";
String destString = "d:\\b.txt";
// method1(srcString, destString);
// method2(srcString, destString);
// method3(srcString, destString);
// method4(srcString, destString);
method5(srcString, destString);
}
//
private static void method5(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
//
private static void method4(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
}
bw.close();
br.close();
}
//
private static void method3(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
int ch = 0;
while ((ch = br.read()) != -1) {
bw.write(ch);
}
bw.close();
br.close();
}
//
private static void method2(String srcString, String destString)
throws IOException {
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(destString);
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
}
fw.close();
fr.close();
}
//
private static void method1(String srcString, String destString)
throws IOException {
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(destString);
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
fw.close();
fr.close();
}
}
画像をコピー
package cn.itcast_01;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
*
*
* :
* , , , 。
* , 。
* 4 , 4 。 4 。
*
* :
* c:\\a.jpg -- FileInputStream -- BufferedInputStream
* :
* d:\\b.jpg -- FileOutputStream -- BufferedOutputStream
*/
public class CopyImageDemo {
public static void main(String[] args) throws IOException {
//
// String srcString = "c:\\a.jpg";
// String destString = "d:\\b.jpg";
// File
File srcFile = new File("c:\\a.jpg");
File destFile = new File("d:\\b.jpg");
// method1(srcFile, destFile);
// method2(srcFile, destFile);
// method3(srcFile, destFile);
method4(srcFile, destFile);
}
//
private static void method4(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
//
private static void method3(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
//
private static void method2(File srcFile, File destFile) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
//
private static void method1(File srcFile, File destFile) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
ArrayListコレクションの文字列データをテキストファイルに格納する
package cn.itcast_02;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
/*
* : ArrayList
*
* :
* ,
* ArrayList 。
* ArrayList , 。
* 。
* 。
*
* :
* ArrayList --
* :
* a.txt -- FileWriter -- BufferedWriter
*/
public class ArrayListToFileDemo {
public static void main(String[] args) throws IOException {
// ( )
ArrayList array = new ArrayList();
array.add("hello");
array.add("world");
array.add("java");
//
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
//
for (String s : array) {
//
bw.write(s);
bw.newLine();
bw.flush();
}
//
bw.close();
}
}
テキストファイルからデータ(動作ごとに文字列データ)をコレクションに読み込み、コレクションを巡回します.
package cn.itcast_02;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/*
* : ( ) ,
*
* :
* ,
* 。
* 。
* 。
*
* :
* b.txt -- FileReader -- BufferedReader
* :
* ArrayList
*/
public class FileToArrayListDemo {
public static void main(String[] args) throws IOException {
//
BufferedReader br = new BufferedReader(new FileReader("b.txt"));
// ( )
ArrayList array = new ArrayList();
//
String line = null;
while ((line = br.readLine()) != null) {
array.add(line);
}
//
br.close();
//
for (String s : array) {
System.out.println(s);
}
}
}
私はテキストファイルにいくつかの名前を保存しています.ランダムに一人の名前を取得するプログラムを書いてください.
package cn.itcast_02;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
/*
* : , 。
*
* :
* A:
* B:
* C:
*/
public class GetName {
public static void main(String[] args) throws IOException {
//
BufferedReader br = new BufferedReader(new FileReader("b.txt"));
ArrayList array = new ArrayList();
String line = null;
while ((line = br.readLine()) != null) {
array.add(line);
}
br.close();
//
Random r = new Random();
int index = r.nextInt(array.size());
//
String name = array.get(index);
System.out.println(" :" + name);
}
}
単極フォルダのコピー
package cn.itcast_03;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* :
*
* :e:\\demo
* :e:\\test
*
* :
* A:
* B: File
* C: File , File
* D: File
*/
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
//
File srcFolder = new File("e:\\demo");
//
File destFolder = new File("e:\\test");
// ,
if (!destFolder.exists()) {
destFolder.mkdir();
}
// File
File[] fileArray = srcFolder.listFiles();
// File , File
for (File file : fileArray) {
// System.out.println(file);
// :e:\\demo\\e.mp3
// :e:\\test\\e.mp3
String name = file.getName(); // e.mp3
File newFile = new File(destFolder, name); // e:\\test\\e.mp3
copyFile(file, newFile);
}
}
private static void copyFile(File file, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
指定したディレクトリの下にある指定ファイルをコピーし、接尾辞名を変更します.
package cn.itcast_04;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
/*
* : , 。
* :.java 。
* :.jad
* :jad
*
* :e:\\java\\A.java
* :e:\\jad\\A.jad
*
* :
* A:
* B: java File
* C: File , File
* D: File
* E:
*/
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
//
File srcFolder = new File("e:\\java");
//
File destFolder = new File("e:\\jad");
// ,
if (!destFolder.exists()) {
destFolder.mkdir();
}
// java File
File[] fileArray = srcFolder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isFile() && name.endsWith(".java");
}
});
// File , File
for (File file : fileArray) {
// System.out.println(file);
// :e:\java\DataTypeDemo.java
// :e:\\jad\DataTypeDemo.java
String name = file.getName();
File newFile = new File(destFolder, name);
copyFile(file, newFile);
}
//
File[] destFileArray = destFolder.listFiles();
for (File destFile : destFileArray) {
// System.out.println(destFile);
// e:\jad\DataTypeDemo.java
// e:\\jad\\DataTypeDemo.jad
String name =destFile.getName(); //DataTypeDemo.java
String newName = name.replace(".java", ".jad");//DataTypeDemo.jad
File newFile = new File(destFolder,newName);
destFile.renameTo(newFile);
}
}
private static void copyFile(File file, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
需要:多極フォルダのコピー
package cn.itcast_05;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* :
*
* :E:\JavaSE\day21\code\demos
* :E:\\
*
* :
* A: File
* B: File
* C: File
* a:
*
* File File
* File
* C
* b:
* ( )
*/
public class CopyFoldersDemo {
public static void main(String[] args) throws IOException {
// File
File srcFile = new File("E:\\JavaSE\\day21\\code\\demos");
// File
File destFile = new File("E:\\");
//
copyFolder(srcFile, destFile);
}
private static void copyFolder(File srcFile, File destFile)
throws IOException {
// File
if (srcFile.isDirectory()) {
//
File newFolder = new File(destFile, srcFile.getName());
newFolder.mkdir();
// File File
File[] fileArray = srcFile.listFiles();
for (File file : fileArray) {
copyFolder(file, newFolder);
}
} else {
//
File newFile = new File(destFile, srcFile.getName());
copyFile(srcFile, newFile);
}
}
private static void copyFile(File srcFile, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
キーボードは5人の学生の情報(名前、国語の成績、数学の成績、英語の成績)を入力して、総点によって高いから低いまでテキストのファイルに保存します
package cn.itcast_06;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
/*
* 5 ( , , , ),
*
* :
* A:
* B:
* TreeSet
* C:
* D: ,
*/
public class StudentDemo {
public static void main(String[] args) throws IOException {
//
TreeSet ts = new TreeSet(new Comparator() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getSum() - s1.getSum();
int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;
int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName())
: num4;
return num5;
}
});
//
for (int x = 1; x <= 5; x++) {
Scanner sc = new Scanner(System.in);
System.out.println(" " + x + " ");
System.out.println(" :");
String name = sc.nextLine();
System.out.println(" :");
int chinese = sc.nextInt();
System.out.println(" :");
int math = sc.nextInt();
System.out.println(" :");
int english = sc.nextInt();
//
Student s = new Student();
s.setName(name);
s.setChinese(chinese);
s.setMath(math);
s.setEnglish(english);
//
ts.add(s);
}
// ,
BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"));
bw.write(" :");
bw.newLine();
bw.flush();
bw.write(" , , , ");
bw.newLine();
bw.flush();
for (Student s : ts) {
StringBuilder sb = new StringBuilder();
sb.append(s.getName()).append(",").append(s.getChinese())
.append(",").append(s.getMath()).append(",")
.append(s.getEnglish());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
//
bw.close();
System.out.println(" ");
}
}
学生クラス
package cn.itcast_06;
public class Student {
//
private String name;
//
private int chinese;
//
private int math;
//
private int english;
public Student() {
super();
}
public Student(String name, int chinese, int math, int english) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
既知のs.txtファイルには、「hcexfgijkamdnoqrzstuvwybpl」という文字列があります.
package cn.itcast_07;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
/*
* s.txt :“hcexfgijkamdnoqrzstuvwybpl”
* , ss.txt 。
*
* :
* A: s.txt
* B: ,
* C:
* D:
* E:
* F: ss.txt
*/
public class StringDemo {
public static void main(String[] args) throws IOException {
// ,
BufferedReader br = new BufferedReader(new FileReader("s.txt"));
String line = br.readLine();
br.close();
//
char[] chs = line.toCharArray();
//
Arrays.sort(chs);
//
String s = new String(chs);
// ss.txt
BufferedWriter bw = new BufferedWriter(new FileWriter("ss.txt"));
bw.write(s);
bw.newLine();
bw.flush();
bw.close();
}
}
BufferedReaderのreadLine()機能をReaderでシミュレート
package cn.itcast_08;
import java.io.IOException;
import java.io.Reader;
/*
* Reader BufferedReader readLine()
*
* readLine(): , , ,
*/
public class MyBufferedReader {
private Reader r;
public MyBufferedReader(Reader r) {
this.r = r;
}
/*
* : , 。
*/
public String readLine() throws IOException {
/*
* , ? r ? ,
* , , ? , , ?
* , 。 , 。
* , , , , 。
* ? , , 。
* , 。 StringBuilder
*/
StringBuilder sb = new StringBuilder();
// , , -1
/*
hello
world
java
104101108108111
119111114108100
1069711897
*/
int ch = 0;
while ((ch = r.read()) != -1) { //104,101,108,108,111
if (ch == '\r') {
continue;
}
if (ch == '
') {
return sb.toString(); //hello
} else {
sb.append((char)ch); //hello
}
}
// , sb 0
if (sb.length() > 0) {
return sb.toString();
}
return null;
}
/*
*
*/
public void close() throws IOException {
this.r.close();
}
}
MyBufferedReaderをテストするときは、BufferedReaderのように使用します.
package cn.itcast_08;
import java.io.FileReader;
import java.io.IOException;
/*
* MyBufferedReader , BufferedReader
*/
public class MyBufferedReaderDemo {
public static void main(String[] args) throws IOException {
MyBufferedReader mbr = new MyBufferedReader(new FileReader("my.txt"));
String line = null;
while ((line = mbr.readLine()) != null) {
System.out.println(line);
}
mbr.close();
// System.out.println('\r' + 0); // 13
// System.out.println('
' + 0);// 10
}
}
BufferedReader
package cn.itcast_09;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
/*
* BufferedReader
* |--LineNumberReader
* public int getLineNumber() 。
* public void setLineNumber(int lineNumber)
*/
public class LineNumberReaderDemo {
public static void main(String[] args) throws IOException {
LineNumberReader lnr = new LineNumberReader(new FileReader("my.txt"));
// 10
// lnr.setLineNumber(10);
// System.out.println(lnr.getLineNumber());
// System.out.println(lnr.getLineNumber());
// System.out.println(lnr.getLineNumber());
String line = null;
while ((line = lnr.readLine()) != null) {
System.out.println(lnr.getLineNumber() + ":" + line);
}
lnr.close();
}
}
package cn.itcast_09;
import java.io.IOException;
import java.io.Reader;
public class MyLineNumberReader {
private Reader r;
private int lineNumber = 0;
public MyLineNumberReader(Reader r) {
this.r = r;
}
public int getLineNumber() {
// lineNumber++;
return lineNumber;
}
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
public String readLine() throws IOException {
lineNumber++;
StringBuilder sb = new StringBuilder();
int ch = 0;
while ((ch = r.read()) != -1) {
if (ch == '\r') {
continue;
}
if (ch == '
') {
return sb.toString();
} else {
sb.append((char) ch);
}
}
if (sb.length() > 0) {
return sb.toString();
}
return null;
}
public void close() throws IOException {
this.r.close();
}
}
package cn.itcast_09;
import java.io.IOException;
import java.io.Reader;
import cn.itcast_08.MyBufferedReader;
public class MyLineNumberReader2 extends MyBufferedReader {
private Reader r;
private int lineNumber = 0;
public MyLineNumberReader2(Reader r) {
super(r);
}
public int getLineNumber() {
return lineNumber;
}
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
@Override
public String readLine() throws IOException {
lineNumber++;
return super.readLine();
}
}
package cn.itcast_09;
import java.io.FileReader;
import java.io.IOException;
public class MyLineNumberReaderTest {
public static void main(String[] args) throws IOException {
// MyLineNumberReader mlnr = new MyLineNumberReader(new FileReader(
// "my.txt"));
MyLineNumberReader2 mlnr = new MyLineNumberReader2(new FileReader(
"my.txt"));
// mlnr.setLineNumber(10);
// System.out.println(mlnr.getLineNumber());
// System.out.println(mlnr.getLineNumber());
// System.out.println(mlnr.getLineNumber());
String line = null;
while ((line = mlnr.readLine()) != null) {
System.out.println(mlnr.getLineNumber() + ":" + line);
}
mlnr.close();
}
}
適用
public class ReadText {
private static Logger logger = LoggerFactory.getLogger(ReadText.class);
public static void main(String[] args) throws FileNotFoundException {
// ApplicationContext cml = new ClassPathXmlApplicationContext("applicationContext.xml");
// BestProccessor bestTransmit = (BestProccessor) cml.getBean("bestProccessor");
File file = new File("D:\\s.txt"); //
StringBuilder result = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));// BufferedReader
String s = null;
while ((s = br.readLine()) != null) {// readLine ,
result.append(System.lineSeparator() + s);
String[] t = s.split(",");
getIntegralBack(t[0],t[1],"", "1");
System.out.println(t[0] + " " + t[1]);
}
br.close();
} catch (Exception e) {
}
}
/**
*
* @param itvAccount
* @param productCode
* @param orderSeq
* @param call
*/
static void getIntegralBack(String itvAccount,String productCode,String orderSeq,String call){
logger.info(" ,infoMsg:{}", " ");
JSONObject object = new JSONObject();
//ITV
object.put("account", itvAccount);
//0 1
object.put("call", call);
//
object.put("orderNo", orderSeq);
//
object.put("prodCode", productCode);
List nameValuePairs = new ArrayList();
StringBuffer buffer = new StringBuffer();
nameValuePairs.add(new BasicNameValuePair("data",object.toJSONString()));
// buffer.append("http://172.24.1.19:9094/tyEgral/front/call/saveOrder.do");
buffer.append("http://10.255.247.77:9094/tyEgral/front/call/saveOrder.do");
StringBuilder dealStr = new StringBuilder();
dealStr.append("http://172.24.1.19:9091/sjEgral/front/call/dealData.do");
JSONObject result = HttpUtils.getJsonString(buffer.toString(), nameValuePairs);
JSONObject ret = HttpUtils.getJsonString(dealStr.toString(), nameValuePairs);
logger.info(" ,infoMsg:{}", " ");
logger.info(" 》》》》》》》》》》》》》》》》》》》》》", ret.getString("result"));
}
}