关于java创建文件,读取文件的问题,在线等,谢谢

luoxuanufo 2008-03-24 09:17:45
现在我要创建一个制定文件类型的文件(如二进制),再创建一个方法读取它,下面是我写的,我不知道怎么设置类型
public void createFile(String relativePath, String fileName, byte[] content)
throws AdaptorException{
FileAdaptor directory = new FileAdaptor();
directory.createDirectory(relativePath);
File file = new File(relativePath, fileName);
if (!file.exists()) {
try {
FileWriter resultFile = new FileWriter(file);
PrintWriter printFile = new PrintWriter(resultFile);
String strContent = content;
printFile.println(strContent);
resultFile.close();
System.out.println("Successful");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
...全文
863 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovezb_lovezfc 2010-07-29
  • 打赏
  • 举报
回复
[color=#99CC00][/color]里刻录机客家话可进口和
kanebluba 2008-03-24
  • 打赏
  • 举报
回复
先转为ASC2码,再转为2进制

public void createFile(String relativePath, String fileName, byte[] content)  
throws AdaptorException{
FileAdaptor directory = new FileAdaptor();
directory.createDirectory(relativePath);
File file = new File(relativePath, fileName);
if (!file.exists()) {
try {
FileWriter resultFile = new FileWriter(file);
PrintWriter printFile = new PrintWriter(resultFile);
String strContent = new String(content); //这句我觉得该这么改
char[] ch=strContent.toCharArray();
for(int i=0;i<s.length();i++)
{
ch[i]=s.charAt(i);

}

String strContext="";
for(int i=0;i<ch.length;i++)
{

int a=(int)ch[i];
strContext=strContext+Integer.toBinaryString(a);


}
resultFile.write(strContent);
printFile.println(strContent);
resultFile.close();
System.out.println("Successful");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
luoxuanufo 2008-03-24
  • 打赏
  • 举报
回复
不好意思,可能大家没有理解我的意思,我要创建一个文件,这个文件的编码方式是二进制,如何创建呢?我写的那个代码我不知道如何设置文件的类型是为二进制
ming206 2008-03-24
  • 打赏
  • 举报
回复
你要文件的读是吧?
(1)好,建立一个读取文件的方法,返回类型是:StringBuffer
(2)写入文件时,你可以自己建立,或使用你已经存在的方法.当然我这个方法匆忙给你弄的,很丑陋.
你自己完善一下.
/**
* 从文件中将数据读出来
* @param jspSoruce
* @return
* @throws FileNotFoundException
* @throws IOException
* @throws Exception
*/
private static StringBuffer getJSPFildContents(String fildSoruce)
throws FileNotFoundException, IOException, Exception {
FileInputStream fis = null;
InputStreamReader isr = null;
String utf_8="UTF8";
//建立读文件对象
Reader reader = null;
//建立装载文件内容的对象
StringBuffer str = new StringBuffer();
try {
fis = new FileInputStream(fildSoruce);
//UTF8
isr = new InputStreamReader(fis, utf_8);
reader = new BufferedReader(isr);
//以字节方式读取
int c = reader.read();
// 从文件中读取一个字节
while (c != -1) // 判断是否已读到文件的结尾
{
c = reader.read();
str.append((char)c);
}
// 关闭文件
reader.close();
isr.close();
fis.close();
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw e;
}
return str;
}
kanebluba 2008-03-24
  • 打赏
  • 举报
回复
public void createFile(String relativePath, String fileName, byte[] content)
throws AdaptorException{
FileAdaptor directory = new FileAdaptor();
directory.createDirectory(relativePath);
File file = new File(relativePath, fileName);
if (!file.exists()) {
try {
FileWriter resultFile = new FileWriter(file);
PrintWriter printFile = new PrintWriter(resultFile);
String strContent = new String(content); //这句我觉得该这么改
resultFile.write(strContent);
printFile.println(strContent);
resultFile.close();
System.out.println("Successful");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧