package io;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class copy {
public static void main(String[] args) {
String filePath;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入信息:");
filePath = br.readLine();
File fileName = new File(filePath);
while(fileName.isDirectory()) {
System.out.println(fileName.getName() + "是一个目录请重新输入");
}
System.out.println(fileName.getName() + "是一个文件:");
try{
System.out.println("创建文件输入流对象……");
FileInputStream fis = FileInputStream(fileName.getName());//这行提示有错误
System.out.println("指定输入文件对象……");
byte data[] = new byte[100];
System.out.println("读入文件数组到指定数组中……");
fis.read(data);
String str = data.toString();
System.out.println("创建文件输出流对象……");
FileOutputStream fos = new FileOutputStream("copy.txt");
System.out.println("将指定数组的内容读入文件中……");
fos.write(str.getBytes());
System.out.println("关闭所有文件");
fis.close();
fos.close();
}
catch(Exception e){}
}}错误提示:Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method FileInputStream(String) is undefined for the type copy请问这个错误应该怎么改啊
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class copy {
public static void main(String[] args) {
String filePath;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入信息:");
filePath = br.readLine();
File fileName = new File(filePath);
while(fileName.isDirectory()) {
System.out.println(fileName.getName() + "是一个目录请重新输入");
}
System.out.println(fileName.getName() + "是一个文件:");
try{
System.out.println("创建文件输入流对象……");
FileInputStream fis = FileInputStream(fileName.getName());//这行提示有错误
System.out.println("指定输入文件对象……");
byte data[] = new byte[100];
System.out.println("读入文件数组到指定数组中……");
fis.read(data);
String str = data.toString();
System.out.println("创建文件输出流对象……");
FileOutputStream fos = new FileOutputStream("copy.txt");
System.out.println("将指定数组的内容读入文件中……");
fos.write(str.getBytes());
System.out.println("关闭所有文件");
fis.close();
fos.close();
}
catch(Exception e){}
}}错误提示:Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method FileInputStream(String) is undefined for the type copy请问这个错误应该怎么改啊
直接这样:FileInputStream fis = FileInputStream(fileName)试试
应该要FileInputStream fis = new FileInputStream(fileName);这样调用吧。
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定,没有通过文件名来构造的。多看看api吧
fileName.getName()获取到的只是文件名,不包含路径的,到时候你会找不到文件的。直接传入fileName就可以了:FileInputStream fis = new FileInputStream(fileName);
运行结果如下:
请输入信息:射雕英雄传
射雕英雄传是一个文件:
创建文件输入流对象……
这个没有错
难道java语法修改了