import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.FileReader
import java.io.IOException
import java.io.PrintWriter
import java.util.Scannerpublic class AddList {
private String filePath = ""
private String bakPath = ""
private String content = ""
Scanner sc = new Scanner(System.in)
public String readFile(){
content = ""
if (isNull(filePath)) {
filePath = sc.nextLine()
}
File file = new File(filePath)
FileReader fr = null
try {
if (file.exists()) {
fr = new FileReader(file)
char[] chars = new char[1024]
int n = 0
while((n = fr.read(chars)) != -1){
String string = new String(chars, 0, n)
content = content + string
}
} else {
System.out.println("文件不存在")
}
} catch (Exception e) {
e.printStackTrace()
} finally {
if (fr != null) {
try {
fr.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
return content
}
public void writeFile(String path){
File file = new File(path)
FileOutputStream fos = null
mkDirs(path)
try {
fos = new FileOutputStream(file)
BufferedOutputStream bos = new BufferedOutputStream(fos)
PrintWriter pw = new PrintWriter(bos, true)
pw.print(content)
pw.flush()
} catch (FileNotFoundException e) {
e.printStackTrace()
} finally {
if (fos != null) {
try {
fos.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
}
public void writeFile(){
if (isNull(filePath)) {
System.out.println("文件存储路径:")
filePath = sc.nextLine()
}
File file = new File(filePath)
FileOutputStream fos = null
mkDirs(filePath)
try {
fos = new FileOutputStream(file)
BufferedOutputStream bos = new BufferedOutputStream(fos)
PrintWriter pw = new PrintWriter(bos, true)
pw.print(content)
pw.flush()
} catch (FileNotFoundException e) {
e.printStackTrace()
} finally {
if (fos != null) {
try {
fos.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
}
public void mkDirs(String filepath){
if (filepath.indexOf("\\") != -1) {
filepath = filepath.replaceAll("\\", "/")
}
int n = filepath.indexOf("//")
String path = filepath.substring(0, n) + "//"
filepath = filepath.substring(filepath.indexOf("//") + 1, filepath.length())
String[] files = filepath.split("/")
for (int i = 0i <files.length - 1i++) {
path = path + files[i]
File file = new File(path)
if (!file.exists()) {
file.mkdir()
}
}
}
public void addImfor(){
System.out.println("--------增加记录---------")
String name = ""
String tel = ""
String email = ""
content = readFile()
while(true){
System.out.println("姓名:")
name = sc.next()
System.out.println("电话:")
tel = sc.next()
System.out.println("Email:")
email = sc.next()
content = content + name + "<>" + tel + "<>" + email +"<==>"
System.out.println("0、Exit 1、继续")
int i = sc.nextInt()
if (i == 0) {
break
}
}
writeFile()
}
public void deleteImfor(){
System.out.println("---------删除记录---------")
String name = ""
String[] imfors = null
content = readFile()
while(true){
System.out.println("你要删除的姓名是:")
name = sc.next()
if (content.indexOf(name) != -1) {
imfors = content.split("<==>")
for (int i = 0i <imfors.lengthi++) {
if (imfors[i].indexOf(name) != -1) {
imfors[i] = ""
}
}
content = ""
for (int i = 0i <imfors.lengthi++) {
if (!isNull(imfors[i])) {
content = content + imfors[i] + "<==>"
}
}
writeFile()
System.out.println("删除成功")
} else {
System.out.println("此人不存在")
}
System.out.println("0、Exit 1、继续")
int i = sc.nextInt()
if (i == 0) {
break
}
}
}
public void viewAll(){
System.out.println("----------显示所有------------")
content = readFile()
if (!isNull(content)) {
String[] imfors = content.split("<==>")
System.out.println("姓名\t电话\tEmail")
for (int i = 0i <imfors.lengthi++) {
String[] imfor = imfors[i].split("<>")
for (int j = 0j <imfor.lengthj++) {
System.out.print(imfor[j] + "\t")
}
System.out.println()
}
} else {
System.out.println("暂时还没有记录")
}
}
public void queryImfor(){
System.out.println("----------查找记录-----------")
content = readFile()
if (!isNull(content)) {
String result = ""
String[] imfors = null
String[] imfor = null
String name = ""
boolean bool = false
while(true){
result = ""
System.out.println("请输入关键字(按姓名查找):")
name = sc.next()
bool = false
if (content.indexOf(name) != -1) {
imfors = content.split("<==>")
for (int i = 0i <imfors.lengthi++) {
if (imfors[i].indexOf(name) != -1) {
imfor = imfors[i].split("<>")
if (imfor[0].equals(name)) {
bool = true
result = result + imfors[i] + "<==>"
}
}
}
if (bool) {
imfors = result.split("<==>")
System.out.println("姓名\t电话\tEmail")
for (int i = 0i <imfors.lengthi++) {
imfor = imfors[i].split("<>")
for (int j = 0j <imfor.lengthj++) {
System.out.print(imfor[j] + "\t")
}
System.out.println()
}
} else {
System.out.println("无此人信息")
}
} else {
System.out.println("无此人信息")
}
System.out.println("0、Exit 1、继续")
int i = sc.nextInt()
if (i == 0) {
break
}
}
} else {
System.out.println("文件还没有记录")
}
}
public void copy(){
System.out.println("----------备份-----------")
content = readFile()
if (isNull(bakPath)) {
System.out.println("备份全路径:")
bakPath = sc.next()
}
writeFile(bakPath)
System.out.println("备份成功")
}
public boolean isNull(String string){
if (null == string || "" == string || 0 == string.length()) {
return true
} else {
return false
}
}
public static void main(String[] args) {
AddList add = new AddList()
Scanner sc = new Scanner(System.in)
int operater = 0
while(true){
System.out.println("选择功能:\n1、增加记录 2、删除记录 3、显示所有 4、查询记录 5、备份 6、退出")
operater = sc.nextInt()
if (1 == operater) {
add.addImfor()
} else if (2 == operater) {
add.deleteImfor()
} else if (3 == operater) {
add.viewAll()
} else if (4 == operater) {
add.queryImfor()
} else if (5 == operater) {
add.copy()
} else if (6 == operater) {
System.out.println("谢谢使用")
break
}
}
}
}
JAVA_HOME环境变量对java开发很重要,mac下也不例外。mac自带jdk,apple维护的jdk,默认的jdk路径,也就是JAVA_HOME路径是:
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
java version “1.6.0_22″
Java(TM) SE Runtime Environment (build 1.6.0_22-b04-307-10M3261)
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03-307, mixed mode)
openjdk7有mac的版本,装好后,路径是:
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
openjdk version “1.7.0-internal”
OpenJDK Runtime Environment (build 1.7.0-internal-b00)
OpenJDK 64-Bit Server VM (build 20.0-b06, mixed mode)
openjdk6也可以运行在mac下,不过不能直接运行,需要有macport。
都安装好后,openjdk6的路径是:
/opt/local/share/java/openjdk6
openjdk version “1.6.0″
OpenJDK Runtime Environment (build 1.6.0-b20)
OpenJDK 64-Bit Server VM (build 17.0-b16, mixed mode)
参考自开源中国社区。
你在终端里面使用 “java -version” 来测试 java 是否已经安装好,如果能得到版本号,那就已经安装好了。可以用 "whereis java" 来查找 java 在哪个目录。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)