mac系统,java编程中文件流的路径是如何写的
推荐于2017-08-17 · 知道合伙人互联网行家
关注
展开全部
看看这个,我昨天刚写的: import java.io.BufferedOutputStream;
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.Scanner;public class AddList {
private String filePath = "";
private String bakPath = "";
private String content = "";
Scanner sc = new Scanner(System.in);
public String readFile(){
content = "";
if (isNull(filePath)) {
System.out.println("文件存储路径:");
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 = 0; i < files.length - 1; i++) {
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 = 0; i < imfors.length; i++) {
if (imfors[i].indexOf(name) != -1) {
imfors[i] = "";
}
}
content = "";
for (int i = 0; i < imfors.length; i++) {
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 = 0; i < imfors.length; i++) {
String[] imfor = imfors[i].split("<>");
for (int j = 0; j < imfor.length; j++) {
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 = 0; i < imfors.length; i++) {
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 = 0; i < imfors.length; i++) {
imfor = imfors[i].split("<>");
for (int j = 0; j < imfor.length; j++) {
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;
}
}
}
}
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.Scanner;public class AddList {
private String filePath = "";
private String bakPath = "";
private String content = "";
Scanner sc = new Scanner(System.in);
public String readFile(){
content = "";
if (isNull(filePath)) {
System.out.println("文件存储路径:");
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 = 0; i < files.length - 1; i++) {
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 = 0; i < imfors.length; i++) {
if (imfors[i].indexOf(name) != -1) {
imfors[i] = "";
}
}
content = "";
for (int i = 0; i < imfors.length; i++) {
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 = 0; i < imfors.length; i++) {
String[] imfor = imfors[i].split("<>");
for (int j = 0; j < imfor.length; j++) {
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 = 0; i < imfors.length; i++) {
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 = 0; i < imfors.length; i++) {
imfor = imfors[i].split("<>");
for (int j = 0; j < imfor.length; j++) {
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;
}
}
}
}
2017-08-17
展开全部
"/Users/sherlockgy/Desktop/文件夹名",
获取路径可以用终端,cd Desktop就是进入桌面,再cd 文件夹名 就是进入其他文件夹,ls可以查看当前目录下的文件/文件夹,pwd显示当前绝对路径
获取路径可以用终端,cd Desktop就是进入桌面,再cd 文件夹名 就是进入其他文件夹,ls可以查看当前目录下的文件/文件夹,pwd显示当前绝对路径
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询