if you would like to use the arguments as file path in the runtime, then don't use System.in.read().....use args[] to stands for the input arguments in the runtime.
the first argument is args[1];
the first argument is args[1];
你说的可行,只是我想从上面问题中知道些为什么,关于File的操作经常出现莫名的问题,想不大通,郁闷:(
import java.lang.SecurityException;
class ReadFile
{
public static void main(String args[]) throws Exception
{
//String ss="d:\\Temp\\Test.java";
//File readFile = new File(ss); StringBuffer stfDir = new StringBuffer();
char ch = (char)System.in.read();
while( ch != '\n')
{
stfDir.append(ch);
ch = (char)System.in.read();
}
File file;
file.createNewFile();
String ss = stfDir.toString();
File readFile = new File(file,ss);
if( readFile.isFile() &&readFile.canWrite())
{
RandomAccessFile rafFile = new RandomAccessFile(readFile,"w");
while(rafFile.getFilePointer()<rafFile.length())
System.out.println(rafFile.readLine());
rafFile.close();
}
else
System.out.println("File cann’t be read!");
// throws SecurityException;
}
}我搞不出来
import java.lang.SecurityException;
class ReadFile
{
public static void main(String args[]) throws Exception
{
//String ss="d:\\Temp\\Test.java";
//File readFile = new File(ss); StringBuffer stfDir = new StringBuffer();
char ch = (char)System.in.read();
//需要排除回车符\r和换行符\n,
//如果不排除\r,当然会出现后面无法找到文件的异常
//while( ch != '\n')
while( ch != '\n' && ch!='\r')
{
stfDir.append(ch);
ch = (char)System.in.read();
}
//下面的语句我给你注释了,好像没有用处
//File file;
//file.createNewFile();
String ss = stfDir.toString();
File readFile = new File(ss);
if( readFile.isFile() &&readFile.canWrite())
{
//注意是"r", "rw", "rws", or "rwd"中的某一个字符串
RandomAccessFile rafFile = new RandomAccessFile(readFile,"rw");
while(rafFile.getFilePointer()<rafFile.length())
System.out.println(rafFile.readLine());
rafFile.close();
}
else
System.out.println("File cann’t be read!");
// throws SecurityException;
}
}运行实例:
F:\java>javac ReadFile.javaF:\java>java ReadFile
t.txt
what i do all for you