首页 > 编程语言 >Java 读取文件

Java 读取文件

时间:2022-11-28 18:00:59浏览次数:45  
标签:文件 saveToFile 读取 else content getContentId Java mmContent String

由于项目的需要,需要写一个解析文件的类。



import java.io.EOFException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URL;
import java.util.Hashtable;

import com.nokia.mms.*;

/**
*
* @author JavaAlpha 解析用户发送的自定义文件,生成原始内容文件
*/

public class SeedAudit
{
MMMessage mms = new MMMessage();
MMDecoder decoder = new MMDecoder();
MMContent content = new MMContent();

public SeedAudit(String file)
{
// 将MMS文件写到byte数组中
byte[] mmsFile = readFile(file);
// 将数组set到content中
content.setContent(mmsFile, 0, mmsFile.length);

decoder.setMessage(mmsFile);

try
{
decoder.decodeMessage();
mms = decoder.getMessage();
storeContents(mms);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MMDecoderException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}


/*// 获取MMS文件的id
String id = content.getContentId();
// 获取MMS文件的type
String type = content.getType();
// 获取MMS文件长度
int length = content.getLength();

System.out.println("id--" + id);
System.out.println("type--" + type);

for (int i = 0; i < length; i++)
{
try
{
if (type.compareTo(IMMConstants.CT_TEXT_PLAIN) == 0)
{
// 如果文件类型是文本文件
content.saveToFile(id + ".txt");
} else if (type.compareTo(IMMConstants.CT_IMAGE_JPEG) == 0)
{
// 如果文件类型是jpg文件
content.saveToFile(id + ".jpg");
} else if (type.compareTo(IMMConstants.CT_IMAGE_GIF) == 0)
{
// 如果文件类型是gif文件
content.saveToFile(id + ".gif");
} else
{
content.saveToFile(id);
}
} catch (IOException e)
{
e.printStackTrace();
}
}*/
}

private void storeContents(MMMessage mm) throws IOException
{
MMContent mmContent = null;
String path = getPath();

for (int j = 0; j < mm.getNumContents(); j++)
{
mmContent = mm.getContent(j);

/*System.out.println(" Content " + j + " --> ID = "
+ mmContent.getContentId() + ", TYPE = "
+ mmContent.getType() + ", LENGTH = "
+ mmContent.getLength());*/

if ((mmContent.getType()).compareTo(IMMConstants.CT_IMAGE_WBMP) == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".wbmp");
else if ((mmContent.getType())
.compareTo(IMMConstants.CT_TEXT_PLAIN) == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".txt");
else if ((mmContent.getType())
.compareTo(IMMConstants.CT_APPLICATION_SMIL) == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".smil");
else if ((mmContent.getType())
.compareTo(IMMConstants.CT_IMAGE_JPEG) == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".jpg");
else if ((mmContent.getType()).compareTo(IMMConstants.CT_IMAGE_GIF) == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".gif");
else if ((mmContent.getType()).compareTo("audio/wav") == 0)
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()) + ".wav");
else
mmContent.saveToFile(path
+ cleanString(mmContent.getContentId()));
}
}

private String cleanString(String str)
{
String result;

if ((str.charAt(0) == '<') && (str.charAt(str.length() - 1) == '>'))
result = str.substring(1, str.length() - 1);
else
result = str;
return result;
}

private String getPath()
{
URL url = getClass().getResource(getClass().getName() + ".class");
String classPath = url.getHost() + url.getFile();
int pos = classPath.lastIndexOf("/");
return classPath.substring(0, pos + 1);
}

// 读取文件
private byte[] readFile(String filename)
{
int fileSize = 0;
RandomAccessFile fileH = null;

// Opens the file for reading.
try
{
fileH = new RandomAccessFile(filename, "r");
fileSize = (int) fileH.length();
} catch (IOException ioErr)
{
System.err.println("Cannot find " + filename);
System.err.println(ioErr);
System.exit(200);
}

// allocates the buffer large enough to hold entire file
byte[] buf = new byte[fileSize];

// reads all bytes of file
int i = 0;
try
{
while (true)
{
try
{
buf[i++] = fileH.readByte();
} catch (EOFException e)
{
break;
}
}
} catch (IOException ioErr)
{
System.out.println("ERROR in reading of file" + filename);
}

return buf;
}

public static void main(String[] args)
{
SeedAudit seed = new SeedAudit("e:/Sample.mms");
}
}

标签:文件,saveToFile,读取,else,content,getContentId,Java,mmContent,String
From: https://blog.51cto.com/javaalpha/5893248

相关文章

  • tomcate 配置 SSL JKS文件
    1.上传jks证书到服务器2.在conf目录下的server.xml添加如下代码:<Connectorport="443"protocol="org.apache.coyote.http11.Http11NioProtocol"maxThr......
  • java.lang.OutOfMemoryError: Java heap space读取文件时出现这个异常
    exceptionorg.apache.jasper.JasperException:javax.servlet.ServletException:java.lang.OutOfMemoryError:Javaheapspaceorg.apache.jasper.servlet.JspServletW......
  • java征程
    疫情三年,生活压力巨大,各个企业面临裁员,团队成本开销巨大,为了稳固发展,只能精简人员,节约成本,故此,很多项目的运维与开发只能亲自上阵,故,重新学习java,再次充实自己,后续会逐步更......
  • java 连连看,正在完善中……
    packageguiGame;importjavax.swing.JFrame;importjavax.swing.SwingUtilities;publicclassGameMain{/***@paramargs*连连看游戏,Java版*/publi......
  • javascript常用技巧
    javascript常用技巧事件源对象event.srcElement.tagNameevent.srcElement.type捕获释放event.srcElement.setCapture(); event.srcElement.releaseCapture(); 事件......
  • 对c#和Java的评述
    自C#诞生之日起,关于C#与Java之间的论战便此起彼伏,至今不辍。抛却Microsoft与Sun之间的恩怨与口角,客观地从技术上讲,C#与Java都是对传统面向对象程序设计在组件化软件时代的革......
  • MyEclipse 8.0注册码+原版下载_Java开发软件
    MyEclipse是一个十分优秀的用于开发Java,J2EE的Eclipse插件集合,MyEclipse的功能非常强大,支持也十分广泛,尤其是对各种开元产品的支持十分不错。MyEclipse目前支持JavaServl......
  • java面试题,上楼梯有多少种方式
     java面试题,上楼梯有多少种方式题目:一个小孩上一个N级台阶的楼梯,他可以一次走1阶、2阶或3阶,那么走完N阶有多少种方式。很自然的想法是使用递归:publicclassTest04{pub......
  • 在某一磁盘目录下将.txt的所有文件遍历打印到控制台上
    packagetest.haofa;//用java语言编写,在某一磁盘目录下将.txt的所有文件遍历打印到控制台上importjava.io.File;importjavax.swing.JOptionPane;publicclassDemoFile{p......
  • java程序员计算机网络面试题大全含答案
    java程序员计算机网络面试题大全含答案java程序员计算机网络面试题大全含答案GET请求请注意,查询字符串(名称/值对)是在GET请求的URL中发送的:/test/demo_form?name1=val......