import java.io.*;
public class Test4 {
public static void main(String[] args) {
String pathname="C:\\Users\\郭炳南\\Desktop\\存储区.txt\\";
String pathname1="C:\\Users\\郭炳南\\Desktop\\存储区1.txt\\";
File file=new File(pathname);
File file1=new File(pathname1);
FileReader fr=null;
FileWriter fw=null;
try {
fr=new FileReader(file);
fw=new FileWriter(file1);
char[] c=new char[10];
int len;
while ((len=fr.read(c))!=-1){
fw.write(c,0,len);
System.out.print(c);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (fw!=null)
fw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
if (fr!=null)
fr.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
在文件中写入商品传入另一个文件中
运行时打印商品信息
1. 苹果 100斤 5元每斤
2. 香蕉 50斤 10元每斤
3. 梨子 80斤 6元每斤
Process finished with exit code 0
import java.io.*;
import java.util.HashMap;
public class Test4 {
public static void main(String[] args) {
char c1='1';
char c2=0;
HashMap<Character,Character> hm=new HashMap<>();
String pathname="C:\\Users\\郭炳南\\Desktop\\存储区.txt\\";
String pathname1="C:\\Users\\郭炳南\\Desktop\\存储区1.txt\\";
File file=new File(pathname);
File file1=new File(pathname1);
FileReader fr=null;
FileWriter fw=null;
try {
fr=new FileReader(file);
fw=new FileWriter(file1);
char[] c=new char[50];
int len;
while ((len=fr.read(c))!=-1){
fw.write(c,0,len);
System.out.print(c);
System.out.println();
}
for (int i=0;i<c.length;i++){
if (c[i]==10){c1=c[i+1];}
if (c[i]=='/'){c2=c[i+1];hm.put(c1,c2);
System.out.print(c1);
System.out.print(c2);
c1=0;
c2=0;
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (fw!=null)
fw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
if (fr!=null)
fr.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
用'/'标记商品价格
利用HashMap将序号和商品价格连接
import java.io.*;
import java.util.HashMap;
public class Test4 {
public static void main(String[] args) {
char c1='1';
char c2=0;
char begain='.';
char end=' ';
int be=0;
int en=0;
int kk=0;//标记s
String[] s=new String[30];
HashMap<Integer,String> hm=new HashMap<>();
HashMap<Integer,Integer> hm1=new HashMap<>();
String pathname="C:\\Users\\郭炳南\\Desktop\\存储区.txt\\";
String pathname1="C:\\Users\\郭炳南\\Desktop\\存储区1.txt\\";
File file=new File(pathname);
File file1=new File(pathname1);
FileReader fr=null;
FileWriter fw=null;
try {
fr=new FileReader(file);
fw=new FileWriter(file1);
char[] c=new char[50];
int len;
while ((len=fr.read(c))!=-1){
fw.write(c,0,len);
System.out.print(c);
System.out.println();
}
for (int i=0;i<c.length;i++){
if (c[i]==begain){be=i;}
if (c[i]==end){en=i;}
if (be!=0&&en!=0){
StringBuffer sb=new StringBuffer();
for (int j=be+1;j<en;j++){
sb.append(c[j]);}
be=0;
en=0;
s[kk]=sb.toString();
// System.out.println(s[kk]);
kk++;
}
int p1=0;
int p2=0;
if (c[i]==10){c1=c[i+1];}
if (c[i]=='/'){c2=c[i+1];
p1=c1-48;
p2=c2-48;
// System.out.println(p2);
hm1.put(p1,p2);
// System.out.print(c1);
// System.out.print(c2);
c1=0;
c2=0;
}
}
for (int i=0;i<s.length;i++){
if (s[i]==null){break;}
// System.out.println(hm1.get('1'));
hm.put(i,s[i]);
}
System.out.println(hm1.get(1));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (fw!=null)
fw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
if (fr!=null)
fr.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
设计两个HashMap
一个将序号和商品名字连接
一个将序号和价格连接