Java文件I / O:为什么我总是得到I / O异常?(Java File I/O: Why do I always get an I/O exception?)
我试图写入一个文件,然后从同一个文件中读取。 输出为“错误:I / O异常”。 意味着程序正在捕获IOException。
public class fileIO { public static void main(String[] args) { // TODO Auto-generated method stub try { File file = new File("io.txt"); BufferedReader read = new BufferedReader(new FileReader(file)); BufferedWriter write = new BufferedWriter(new FileWriter(file)); String needs = "This is going to the file"; write.write(needs); String stuff = read.readLine(); while(stuff != null) { System.out.println(stuff); stuff = read.readLine(); } } catch(IOException e) { System.out.println("Error: I/O Exception"); } catch(NullPointerException e) { System.out.println("Error: NullPointerException"); } } }'
I am trying to write to a file and then read from that same file. The output is "Error: I/O exception". Meaning that the program is catching the IOException.
public class fileIO { public static void main(String[] args) { // TODO Auto-generated method stub try { File file = new File("io.txt"); BufferedReader read = new BufferedReader(new FileReader(file)); BufferedWriter write = new BufferedWriter(new FileWriter(file)); String needs = "This is going to the file"; write.write(needs); String stuff = read.readLine(); while(stuff != null) { System.out.println(stuff); stuff = read.readLine(); } } catch(IOException e) { System.out.println("Error: I/O Exception"); } catch(NullPointerException e) { System.out.println("Error: NullPointerException"); } } }'
原文:https://stackoverflow.com/questions/27028559
满意答案
为了顺利完成,你应该使用一个
Timer
。 假设帧速率是24 fps,则意味着每1/24 = 0.0416秒或大约42毫秒是一帧。 因此,如果您的计时器每42ms计时一次,您可以向后移动mePlayer.Position
:XAML:
<MediaElement x:Name="mePlayer" Source="C:\Sample.mp4" LoadedBehavior="Manual" ScrubbingEnabled="True"/>
码:
System.Windows.Threading.DispatcherTimer dispatcherTimer; int t = 240000; // 4 minutes = 240,000 milliseconds public MainWindow() { InitializeComponent(); dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); //tick every 42 millisecond = tick 24 times in one second dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 42); } private void dispatcherTimer_Tick(object sender, EventArgs e) { // Go back 1 frame every 42 milliseconds (or 24 fps) t = t - 42; mePlayer.Position = TimeSpan.FromMilliseconds(t); } private void Window_Loaded(object sender, RoutedEventArgs e) { mePlayer.Play(); } private void Button_Click(object sender, RoutedEventArgs e) { // Pause and go to 4th minute of the video then start playing backward mePlayer.Pause(); mePlayer.Position = TimeSpan.FromMinutes(4); dispatcherTimer.Start(); }
To do it smoothly you should use a
Timer
. Assuming the frame rate is 24 fps, then it means that is one frame every 1/24 = 0.0416 second or approximately 42 millisecond. So if your timer ticks every 42ms you can movemePlayer.Position
backward:XAML:
<MediaElement x:Name="mePlayer" Source="C:\Sample.mp4" LoadedBehavior="Manual" ScrubbingEnabled="True"/>
Code:
System.Windows.Threading.DispatcherTimer dispatcherTimer; int t = 240000; // 4 minutes = 240,000 milliseconds public MainWindow() { InitializeComponent(); dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); //tick every 42 millisecond = tick 24 times in one second dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 42); } private void dispatcherTimer_Tick(object sender, EventArgs e) { // Go back 1 frame every 42 milliseconds (or 24 fps) t = t - 42; mePlayer.Position = TimeSpan.FromMilliseconds(t); } private void Window_Loaded(object sender, RoutedEventArgs e) { mePlayer.Play(); } private void Button_Click(object sender, RoutedEventArgs e) { // Pause and go to 4th minute of the video then start playing backward mePlayer.Pause(); mePlayer.Position = TimeSpan.FromMinutes(4); dispatcherTimer.Start(); }
相关问答
更多如何在WPF中播放声音?(How To Play Sound In WPF?)
如何在WPF中向后播放视频?(How to play video backwards in WPF?)
视频资源无法在wpf mediaelement中播放(video resource won't play in wpf mediaelement)
如何向后播放音频?(How to play audio backwards?)
WPF:为什么MediaElement不播放?(WPF: Why MediaElement does not play?)
WPF中的视频缩略图(Video thumbnails in WPF)
WPF媒体元素视频源(WPF Media Element Video Source)
在WPF应用程序中嵌入flv(flash)播放器(Embedding flv (flash) player in WPF app)
向后播放视频(Play video backwards)
在WPF中集成Vimeo的视频(Integration of Vimeo's video in WPF)
相关文章
更多Hadoop的I/O
Hadoop I/O系统介绍
Hadoop1.0.4 HDFS I/O性能测试
Java中如何用I/O流读取一个Word文档的内容?
I18n的一个问题
I18N 国际化 简介
《Java 2 图形设计卷I:AWT》(Graphic Java 1.2 Mastering the JFC Volume I:AWT )扫描版[PDF]
Java 流(Stream)、文件(File)和IO
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!