import java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileReader
import java.io.IOException
import java.io.InputStreamReader
public class ReadFile
{
public static void main(String[] args)
{
//文件位置
String filepath = "D:\\test.pub"
/** 一次读取所有内容 */
FileInputStreamReadFile(filepath)
System.out.println("=====================")
/** 以行为单位读取文件,常用于读面向行的格式化文件 */
BufferedReaderReadFile(filepath)
System.out.println("=====================")
/** 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 */
ReadFileByByte(filepath)
System.out.println("\n=====================")
/** 以行为单位读取文件,常用于读面向行的格式化文件 */
InputSteamReaderReadFile(filepath)
System.out.println("\n=====================")
}
private static void InputSteamReaderReadFile(String filepath)
{
try
{
InputStreamReader sr = new InputStreamReader(new FileInputStream(new File(filepath)))
int temp = 0
while ((temp = sr.read()) != -1)
{
System.out.print((char)temp)
}
sr.close()
}
catch (FileNotFoundException e)
{
e.printStackTrace()
}
catch (IOException e)
{
e.printStackTrace()
}
}
private static void ReadFileByByte(String filepath)
{
try
{
File file = new File(filepath)
FileInputStream fis = new FileInputStream(file)
int b = 0
while ((b = fis.read()) != -1)
{
System.out.print((char)b)
}
fis.close()
}
catch (FileNotFoundException e)
{
e.printStackTrace()
}
catch (IOException e)
{
e.printStackTrace()
}
}
private static void BufferedReaderReadFile(String filepath)
{
try
{
StringBuffer sb = new StringBuffer()
BufferedReader br = new BufferedReader(new FileReader(new File(filepath)))
String readLine = ""
while ((readLine = br.readLine()) != null)
{
sb.append(readLine + "\n")
}
br.close()
System.out.print(sb.toString())
}
catch (FileNotFoundException e)
{
e.printStackTrace()
}
catch (IOException e)
{
e.printStackTrace()
}
}
private static void FileInputStreamReadFile(String filepath)
{
try
{
File file = new File(filepath)
FileInputStream fis = new FileInputStream(file)
long filelength = file.length()
byte[] bb = new byte[(int)filelength]
fis.read(bb)
fis.close()
System.out.println(new String(bb))
}
catch (FileNotFoundException e)
{
e.printStackTrace()
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
不知道你的文件格式,不过你可以可以尝试用io流来读取。下面代码 我试过是可以读取挺多格式文件的,你试下。
存取图片就是二进制数据的存取问题把图片以文件的时候读入到程序中
转换成byte
以byte显示保存到数据库中
另外,access保存文件~~不合适~
---------------------------
显示和存储没关系,看你要怎么显示了~显示到浏览器?