首页 \ 问答 \ Java文件I / O:为什么我总是得到I / O异常?(Java File I/O: Why do I always get an I/O exception?)

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
更新时间:2023-11-20 12:11

最满意答案

为了顺利完成,你应该使用一个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 move mePlayer.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的MediaElement。 有关更多信息,请访问官方文档: https : //msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement(v = vs.100).aspx You can use WPF's MediaElement for this. For more information, visit the official documentation: https://msdn.microsoft. ...
  • 为了顺利完成,你应该使用一个Timer 。 假设帧速率是24 fps,则意味着每1/24 = 0.0416秒或大约42毫秒是一帧。 因此,如果您的计时器每42ms计时一次,您可以向后移动mePlayer.Position : XAML: 码: System.Win ...
  • 您不能将视频作为资源嵌入到程序集中,而是从MediaElement中播放它。 https://social.msdn.microsoft.com/Forums/vstudio/en-US/150cb4e7-9107-4d07-8c81-6c2ef9734529/how-to-play-a-wmv-file-as-a-embed-resource-在-resourceresx与- MediaElement的-而不是内容?论坛= WPF 您可能能够做的最接近的事情是将文件添加为content并从文件系统中播放, ...
  • 通常,在使用ASBD时,这些字段描述了由此描述表示的缓冲区中的样本数据的完整布局 - 其中通常这些缓冲区由AudioBufferList中包含的AudioBuffer表示。 但是,当ASBD具有kAudioFormatFlagIsNonInterleaved标志时,AudioBufferList具有不同的结构和语义。 在这种情况下,ASBD字段将描述列表中包含的一个AudioBuffers的格式,并且列表中的每个AudioBuffer被确定为具有单个(单声道)音频数据通道。 然后,ASBD的mChannel ...
  • 媒体源应该来自您的文件系统示例 mediaElement.Source = new Uri(@"C:\User\Admin\Images\Wildlife.wmv") the media source should be from your file system example mediaElement.Source = new Uri(@"C:\User\Admin\Images\Wildlife.wmv")
  • 您可以通过将MediaPlayer的ScrubbingEnabled属性设置为true来实现此目的,并且您仍需要继续前进以实时捕获任何电影。 有关更多样本,请阅读Clint Rutkas撰写的这篇精彩博客文章: http://www.betterthaneveryone.com/archive/2009/10/02/882.aspx You do this by setting the MediaPlayer's property of ScrubbingEnabled to true and still ...
  • 这对我有用。 添加LoadedBehavior =“手动” 然后在后面的代码中你需要播放媒体 bgvideo.Play() 你还需要丢掉uri中的第一个'/'。 心连心 This works for me. Add LoadedBehavior="Manual"
  • 如果embed代码包含iframe ,请使用iframe的src并将其放入WebBrowser源代码中。 例如,对于Youtube视频,这是嵌入代码: 您将在WPF WebBrowser中使用此URL: http://www.youtube.co ...
  • 问题在于输出URL,这是正确的方法 import UIKit import AVFoundation import AVKit class ViewController: UIViewController { var playerViewController = AVPlayerViewController() var playerView = AVPlayer() override func viewDidAppear(_ animated: Bool) { ...
  • WPF WebBrowser控件使用其窗口中嵌入的Internet Explorer ActiveX控件。 因此,它与安装的Internet Explorer版本具有相同的限制。 默认情况下,它将使用IE 7兼容性,因此更现代的JavaScript可能无法正常工作。 您可以通过在HTML中使用固定的DOCTYPE (但显然,您不控制Vimeo的HTML)或通过设置注册表键在计算机上启动可执行文件来强制控件使用更现代的渲染引擎在这篇微软文章中: 浏览器仿真 您需要为.exe名称添加一个条目。 例如,如果您的应 ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)