java中文件路径什么写 java服务器端““文件路径如何书写
大家好,java中文件路径什么写相信很多的网友都不是很明白,包括java服务器端“/“文件路径如何书写也是一样,不过没有关系,接下来就来为大家分享关于java中文件路径什么写和java服务器端“/“文件路径如何书写的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!
mac系统,java编程中文件流的路径是如何写的
看看这个,我昨天刚写的: 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;
}
}
}
}
java服务器端“/“文件路径如何书写
在java中路径都这么写:\\,这个就是路径分隔符,以后就不用“/”了吧
不管是实在windows还是在linux中,你都用“\\”作为路径分隔符就对了
File.separator()这个方法是没什么问题,但是,你如果传入字符串后,
使用File.separator来split,可能会出错的哦,你在所有的地方,都用“\\”,
肯定是不会错的啦。
至于你说的json中也是,在java中就是用\\,这与json无关,而是转义导致的
java文件读写的文件应该放哪个路径
1。如果你用的eclipse,那么
project>>properties>>打开属性设置窗口
有一项java build path,点开
最下面有个default output folder,那里有路径,比如:axis2
如果你要读的文件名字为1.txt的话
你就把它放到C:\eclipse\workspaces\axis2\下面就行了。
2。如果你直接用cmd javac编译的
那么假设你的java文件放在*axis2\Exam9\FileInputStream.java
编译好的class文件也在同一个目录
执行程序要在*axis2\目录
java Exam9.FileInputDemo
而你的文件要放在*axis2\Exam9\
3。也可以用绝对路径。。。
好了,关于java中文件路径什么写和java服务器端“/“文件路径如何书写的问题到这里结束啦,希望可以解决您的问题哈!